Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Criteria API on fields without mapping

 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Benjamin Samuel
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic