Java Not Converting String to Long Object Properly

We're using Spring/Hibernate on a Websphere Application Server for AIX. On my Windows machine, the problem doesn't occur--only when running off AIX. When a user logs in with an account number, if they prefix the '0' to their login ID, the application rejects the login. In the DB2 table, the column is of numeric type, and there shouldn't be a problem converting '090....' to '90...'

Anyone else experience a problem like this? Both machines have Java v1.5.

To be more specific, the flow is FormView -> LoginValidator -> LoginController

In LoginValidator, the value of login is null with the prefixed 0. Without the 0, the value is what it should be (But again, this is only on the AIX environment--on 2 Windows environments it's fine). Here's the snippet of code where the object equals null..

public class LoginValidator implements Validator  {

    public boolean supports(Class clazz) {
    return Login.class.equals(clazz);

    public void validate(Object obj, Errors errors) {
        System.out.println("Inside LoginValidator");
        Login login = (Login) obj;
        //null value
        System.out.println("Before conversion in Validator, store id = " 
              + login.getStoreId()); 

I've also written this short Java program for constructing a Long from a String, and using the java binary that is packaged with WebSphere

public class String2Long {
    public static void main(String[] args){
        String a = "09012179";
        String b = "9012179";

        Long _a = new Long(a);
        Long _b = new Long(b);

        System.out.println(a + " => " + _a); //09012179 => 9012179
        System.out.println(b + " => " + _b); //9012179 => 9012179
        System.out.println("_a.equals(_b) " + _a.equals(_b)); //_a.equals(_b) true


Asked by: Ryan463 | Posted: 23-01-2022

Answer 1

Well there's an awful lot of things going on there. You really need to try to isolate the problem - work out what's being sent to the database, what's being seen by Java etc.

Try to pin it down in a short but complete program which just shows the problem - then you'll be in a much stronger position to file a bug or fix your code.

Answered by: David192 | Posted: 24-02-2022

Answer 2


A co-worker did some research on Spring updates, and apparently this error was correct in v. 2.5.3:

CustomNumberEditor treats number with leading zeros as decimal (removed unwanted octal support while preserving hex)

We were using Spring 2.0.5. We simply replaced the jars with Spring 2.5.4, and it worked as it should have!

Thanks to everyone for your help/assistance. We will make use of Unit tests in the future, but this just turned out to be a Spring bug.

Answered by: Emily596 | Posted: 24-02-2022

Answer 3

Trace through the program following the path of the String all the way to database and make unit tests for every single method on that path. And don't just take the shortest possible route here, make multiple unit tests with different inputs and expected outputs to really see what went possibly wrong. Assuming you don't find any errors, run the same unit tests on the other computer and you should be able to pinpoint the bug. From the top of my head I'd assume it may have something to do with case sensitivity but there really is no way to be sure.

Next time, use TDD.

Answered by: Julia777 | Posted: 24-02-2022

Answer 4

I don't know much about Java, but this might happen the string is interpreted as octal string because of the leading "0".

You can probably work around this using Long.parseLong(a, 10).

Answered by: Patrick209 | Posted: 24-02-2022

Similar questions

A tool for converting database tables to entity java classes for hibernate

I need a tool for converting database tables to entity java files for hibernate. There are 20 tables (mysql) and I don't want to copy table's column name to my Java file and so on. Any suggestions ? Thanks.

java - what is a good pattern for converting between hibernate entities and data transfer objects?

I have had similar questions and concerns as to how to convert between Hibernate entities and data transfer objects to be returned by a web service as are discussed in this question: Is using data transfer objects in ejb3 considered best practice One of the factors mentioned here is that...

java - Converting MySQL query to Hibernate Criteria

SELECT * FROM test.pchi new INNER JOIN rlhi old ON new_id = old.menu_id where'?' Similar to: Select * from db.employee emp INNER JOIN db.table on emp_tableID = where = '?' If you could tell me how to do a projection, that would be awesome... as in: Select,, table.brand from .... I tried...

java - Converting varchar to integer on Oracle using Hibernate

In My Project Score field in Database is Varchar datatype, but it contains integer values now i need to fetch the records having score between 70 to 50. (i.e less than 70). My Query is Query count_query = stmt1.query().from(selectedTable).selectCount(). where(districtcolumn).equals(district_code). and(score).isNull().or(score).lessthan("70").toQuery(); trying to us to_nu...

java - converting hibernate objects to json using GSon

Im trying to convert a collection (obtained from Hibernate) to json string using Gson for use in my restful service. But, the service is giving me http 500 error. My code is as under: @GET @Path("/getScheduleLookUp") @Produces(MediaType.APPLICATION_JSON) public String getScheduleLookUp() throws IOException { ScheduleLookupDAO lookupDAO = new ScheduleLookupDAO(); return new Gson().toJson(look...

java - Converting Set to List in Hibernate Mapping

Following are my pojo classes in BillDetails I used billProductDetailses Set. Now insted of using Set I would Like to use List. For that I am changed in BillDetails.hbm.xml but I am facing problem while mapping in to list. public class BillDetails implements { private Long billNo; private CustomerDetails customerDetails; private Float subTotal; pr...

java - Converting mysql query into hibernate query

I have a MySQL query, and I want to convert this query in to Hibernate Query. This is what I've tried so far. userInQuery = "SELECT * FROM log where userName in("; userCountQuery = "SELECT count(*) FROM log where userName in("; for (int i = 0; i < users.length; i++) { userInQuery += "'" + users[i] + "',"; userCountQuery += "'" + users[i] + "',"; } userInQuery = userInQuery.substring(0, userInQu...

java - Converting hibernate entities, using a pojo that will be composed of numerous entites of different types

I have ComplexCustomerResponse (not persisted), it is composed of data from various sources - CheeseEntity, EmployeeEntity, VehicleEntity. The entities are hibernate entities and shared across multiple projects. To map/convert the data, I want to create a TemporaryHoldingObject that will look like: temporaryHoldingObject.setCheeseEntity(); temporar...

java - Issue converting SQL query to Hibernate query

I am a Hibernate newbie and I need to convert my SQL query to Hibernate format: SELECT as id, as name, pr.brand as brand, pr.age as age, pi.image_url as imageUrl FROM product pr JOIN product_image pi ON ( and pi.image_url IN (SELECT t0.image_url FROM product_image AS t0 ...

java - Hibernate query to get server time without converting to local timezone

I've searched high and low for an answer to this and I've found nothing. Note (before someone refers me to a similar, but unhelpful question) that I'm NOT STORING ANY DATES/TIMES IN THE DATABASE. Background I have a Postgres database that's running on a server somewhere. I have a Java back end that queries the database using Hibernate. The database server and the Java back end (Tomcat serve...

Different answer when converting a Double to an Int - Java vs .Net

In C# if I want to convert a double (1.71472) to an int then I get the answer 2. If I do this in Java using intValue() method, I get 1 as the answer. Does Java round down on conversions? Why do the Java API docs have such scant information about their classes i.e. Returns the value of the specified number as an int. This may involve rounding or truncation. ...

Is there any java library for converting document from pdf to html?

Open source implementation will be preferred.

java - A good library for converting PDF to TIFF?

Closed. This question does not meet Stack Overflow guid...

Converting Oracle Forms 10 to Java

There are a number of commercial products available: Exodus Evo Also Oracle Application Express (APEX) is releasing a

Converting a generic argument to an int in java, provided that it is a number

Here's what I've been trying to do, in a nutshell: class example <T extends Number> { private int function(T number) { int x = (int) number; ... } ... } Basically, I'm trying to make it so that T is a number so I can convert it to an int inside that function. The problem is that I'm getting an "incovertible types" error, so I must be doing something wr...

java - Converting an array of Pixels to an image in C#

I have an array of int pixels in my C# program and I want to convert it into an image. The problem is I am converting Java source code for a program into equivalent C# code. In java the line reads which displays the array of int pixels into image: Image output = createImage(new MemoryImageSource(width, height, orig, 0, width)); can someone tell me the C# equivalent?

java - converting strings to Title case in JSTL

Is there any way to convert a string to Title case, using JSTL tags? Thanks in Advance.

Java library for converting XML to syntax colored HTML

I have a Java string of XML content. I use Velocity to generate some HTML reports, and this XML needs to be included into one of those HTML files. It would be nice if this XML is syntax colored and formatted. Does anyone know of a Java library to do this?

(Java) Specify number of bits (length) when converting binary number to string?

I'm trying to store a number as a binary string in an array but I need to specify how many bits to store it as. For example, if I need to store 0 with two bits I need a string "00". Or 1010 with 6 bits so "001010". Can anyone help? EDIT: Thanks guys, as I'm rubbish at maths/programming in general I've gone with the simplest solution which was David's. Something like:

Converting a Java collection into a Scala collection

Related to Stack Overflow question Scala equivalent of new HashSet(Collection) , how do I convert a Java collection (java.util.List say) into a Scala collection List? I am actually trying to convert a Java API call to Spring's SimpleJdbcTempla...

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)