-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Criteria query not handling Long column datatype
PostPosted: Mon Oct 07, 2013 11:39 am 
Newbie

Joined: Tue Jul 05, 2011 7:28 am
Posts: 10
Hi guys,

I have an entity which has a field on it which is of type Long e.g.:

Code:
@Entity
public class MyEntity {
    private Long myLongField;
    ...
}

I then use the JPA Criteria API to generate a query to find MyEntity's by myLongField e.g.
Code:
criteriaBuilder.equal(myEntityRoot.get("myLongField"), 12345678);
...


What I am seeing is that Hibernate generates an SQL query with the long value with an "L" appended onto the end of it, e.g.:
Code:
select ... from myentity myentity0_ where (myentity0_.mylongfield = 12345678L);

This obviously causes an exception to be thrown:
Code:
MySQLSyntaxErrorException: Unknown column '12345678L' in 'where clause'


I am using Hibernate-3.5.6-Final and MySQL 5.1

Any ideas on how to stop the "L" being appended onto the end of the long value?

Thanks,
Mark


Top
 Profile  
 
 Post subject: Re: Criteria query not handling Long column datatype
PostPosted: Tue Oct 15, 2013 5:33 am 
Newbie

Joined: Tue Jul 05, 2011 7:28 am
Posts: 10
I'll answer my own question here incase any one has a similar issue...

The CriteriaBuilder.equal method will use a LiteralExpression when the second argument to the equal method is an Object. The LiteralExpression uses a LongValueHandler to render the Long value and it is this handler that adds the "L" to the value (it will add F for floats, D for decimals, etc.).

The solutions I used are to either use a ParameterExpression for the long value or write your own NumericExpression which will render a Number value without adding "L', "F", "D", etc. Then pass this Expression object as being the second argument to the CriteriaBuilder.equal method

-Mark


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.