File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes hibernate criteria like expression for Long Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate criteria like expression for Long" Watch "hibernate criteria like expression for Long" New topic

hibernate criteria like expression for Long

siva swamy

Joined: Jul 12, 2007
Posts: 7

I am using Hibernate criteria for search purposes. I have an option to search the objects using its id.(primary key). This id search should be like search and not exact search. i.e If i give 1, then all objects having key start with 1 should be displayed. In my pojo this id is Long. (if i concat % to this key and add it in expression , i got error)
So how can i do this?

Thank you,
Walter Bernstein
Ranch Hand

Joined: Dec 19, 2007
Posts: 57
Use the str() expression:
siva swamy

Joined: Jul 12, 2007
Posts: 7
Thank you, but iam not writing any HQL. By means of criteria how can we do it.
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

Like Walter said, you will need some sort of function to convert the long to a str. Because a like expression against a long really doesn't make much sense, and more so if it is searching PK values. Primary Keys shouldn't have any business logic in them, unless it is a legacy database that you are stuck with.

But either way, you can only get what you are looking for by converting the Long into a String. And I recommend using a Query object instead of the Criteria object, in this case.

I can't remember if Criteria passes things it doesn't understand straight through to the database like a Query object will do. Basically what this would mean is that if you used a function that was database specific that the Hibernate parser didn't understand, it would pass it "as is" to the database.


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: hibernate criteria like expression for Long
It's not a secret anymore!