Sleep criteria for an object with embedded objects

I have an entity called "UserDetails" that has the following variables:

1) String userId 2) String userName 3) UserContact userContact (where UserContact is the class Embeddable)

UserContact has the following variables:

1) String phone number 2) Email String 3) String city

What will be the sleeping criteria for getting the following list:

Users with username = 'sam' and with city = 'New York'

I tried the following and received a run-time exception that did not recognize the 'city' variable:

List<UserLogin> list = session.createCriteria(UserLogin.class).add(Restrictions.eq("userName","sam")).add(Restrictions.eq("city", "New York")).list(); 
+8
orm hibernate hibernate-criteria
source share
1 answer

Oh, I get it ...

 List<UserLogin> list = session.createCriteria(UserLogin.class).add(Restrictions.eq("userName","sam")).add(Restrictions.eq("userContact.city", "New York")).list(); 

Stupidly, you just need to add 'userContact.city' instead of 'city', where userContact is an object of the UserContact class in my entity.

+10
source share

All Articles