“where exists” in Hibernate HQL

前端 未结 2 1141
情话喂你
情话喂你 2020-12-17 07:33

How can I write a "not exists" query in HQL? I am trying to get an HQL not exists query which returns the same results as this Oracle SQL query:



        
相关标签:
2条回答
  • 2020-12-17 08:19

    Try this:

    from School s where (select count(st) from Student st
      where st.school_id=s.id and st.status.id not in (0,1,2,3,4)) = 0
    
    0 讨论(0)
  • 2020-12-17 08:33

    Your named query is not valid (school_id is not a property of the Student entity), which prevents the SessionFactory from being instantiated. You need to think object and associations, not columns. Try this instead:

    from School as s
    where not exists (
      from Student as st
      where st.school = s
      and st.status.id not in (0,1,2,3,4)
    )
    

    References

    • Hibernate Core Reference Guide
      • 14.13. Subqueries
    0 讨论(0)
提交回复
热议问题