Sleep Mode Criteria / Request Object Properties

I have an AppUser class;

 class AppUser { private String firstName; private String lastName; //-- getters and setters } 

I also have another Student class;

 class Student { private AppUser appUser; private Date dateOfBirth; //-- getters and setters } 

How do I search Student John Doe , firstName John, lastName Doe?

If it were a date of birth, I would create a Criteria and add an equality constraint ( Restristions.eq ) to the date. How to do this for lastName and firstName in an AppUser object?

+7
java hibernate criteria
source share
2 answers

Query:

 Query q = session.createQuery( "SELECT s from Student s WHERE s.appUser.firstName=:firstName AND s.appUser.lastName=:lastName"); q.setParameter("firstName", "John"); q.setParameter("lastName", "Doe"); 

To use criteria, mark this stream.

Also see this page from hibernate docs

+7
source share

You may need to add an alias ... something like:

 List students = session.createCriteria(Student.class).createAlias("appUser", "user").add(Restrictions.eq("user.firstName", firstName)).list(); 

Without an alias:

 List students = session.createCriteria(Student.class).add(Restrictions.eq("appUser.firstName", firstName)).list(); 
+10
source share

All Articles