Hibernate Criteria / Query on object properties

前端 未结 2 698
迷失自我
迷失自我 2021-02-06 06:04

I have a class AppUser;

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

I a

相关标签:
2条回答
  • 2021-02-06 06:18

    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");
    

    For using Criteria, check this thread

    Also take a look at this page from hibernate docs

    0 讨论(0)
  • 2021-02-06 06:23

    You might 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();
    
    0 讨论(0)
提交回复
热议问题