File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Criteria API on fields without mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Criteria API on fields without mapping" Watch "Criteria API on fields without mapping" New topic
Author

Criteria API on fields without mapping

Benjamin Samuel
Ranch Hand

Joined: Dec 15, 2006
Posts: 33
Consider the following bean class:



Assume the hbm file have the mapping for the first 3 fields viz. id, name and yearOfBirth. Field 'age' is calculated based on the yearOfBirth and is set via setYearOfBirth() (Age is not stored in DB). I need a solution using Criteria API (org.hibernate.Criteria) to search on Person objects based on 'age' field. I'm trying the below code; but it raises an exception: org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException (could not resolve property: age of: Person)


Any solutions ?

Thanks,
Binil
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

All queries in Hibernate will generate SQL so they must be queries against mapped properties. There's no way of getting round that.

Why not change your query? Something like:

will be logically the same.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Benjamin Samuel
Ranch Hand

Joined: Dec 15, 2006
Posts: 33
My actual problem is little more complicated than this

So is it not possible to do the Criteria check on 'age' field?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No. The steps Hibernate will take is to run a query then populate the properties of your object with the values in the result set. The Criteria will be used to generate the SQL, what you are after is logic applied after the query has been run.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Criteria API on fields without mapping