Skip to main content

Posts

Showing posts from December, 2015

Hibernate query a foreign key field with ID

Hibernate "nicely wraps" only what you tell it to wrap. So, assuming that your  Employee  mapping looks something like: @Entity public class Employee { ... @ManyToOne @JoinColumn ( name = "address_id" ) private Address address ; ... } and your  Address  has an  id  property, you can query based on  address_id  via: session . createCriteria ( Employee . class ) . add ( Restrictions . eq ( "address.id" , addressId )); In order to query based on  Address  properties, you'll have to  create aliases or nested criteria : session . createCriteria ( Employee . class )   . createAlias ( "address" ,   "a" )   . add ( Restrictions . eq ( "a.postalCode" ,  postalCode )); http://stackoverflow.com/questions/1787086/hibernate-query-a-foreign-key-field-with-id http://www.mkyong.com/hibernate/hibernate-criteria-examples/