I have an entity "UserDetails" which has the following variables:
UserContact has the following variables:
What will be a Hibernate Criteria for fetching the following list:
Users with userName = 'sam' and with city = 'New York'
I tried the following and got the runtime exception that it doesn't recognize the variable 'city':
List<UserLogin> list = session.createCriteria(UserLogin.class)
.add(Restrictions.eq("userName","sam"))
.add(Restrictions.eq("city", "New York"))
.list();
The embedded class is embedded in an entity and is mapped to the same database table as the entity. Therefore, unlike the entity, the embedded class does not have a primary key. When using the embedded class, the user specifies @Embeddable in the embedded class.
Criteria cr = session. createCriteria(Employee. class); Criterion salary = Restrictions.gt("salary", 2000); Criterion name = Restrictions. ilike("firstNname","zara%"); // To get records matching with OR conditions LogicalExpression orExp = Restrictions.or(salary, name); cr.
With Hibernate we can use the @Embeddable annotation to mark a class to be eligible as an embeddable class. We can use the @Embedded annotation to embed an embeddable class. The attributes of an embedded class can be overridden using the @AttributeOverrides and the @AttributeOverride annotations.
The Criteria interface makes it easy to selectively fetch the data on the basis of conditions in the select query. The Restriction class in hibernate provide several methods that can be used as conditions (also known as Criterion). These conditions are added to a criteria object with the add() method.
Oh I figured it out...
List<UserLogin> list = session.createCriteria(UserLogin.class)
.add(Restrictions.eq("userName","sam"))
.add(Restrictions.eq("userContact.city", "New York"))
.list();
Silly, just needed to add 'userContact.city' instead of 'city', where userContact is the object of the class UserContact in my entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With