Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate criteria like expression for Long

 
siva swamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the str() expression: http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
 
siva swamy
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, but iam not writing any HQL. By means of criteria how can we do it.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic