This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.