aspose file tools*
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
Author

hibernate criteria like expression for Long

siva swamy
Greenhorn

Joined: Jul 12, 2007
Posts: 7
Hi,

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,
Regards,
K.Siva
Walter Bernstein
Ranch Hand

Joined: Dec 19, 2007
Posts: 57
Use the str() expression: http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
siva swamy
Greenhorn

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
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate criteria like expression for Long