HQL Query to check if size of collection is 0 or empty
Using IS EMPTY
should work (I would favor a JPQL syntax):
SELECT u FROM User u WHERE u.status = 1 AND u.appointments IS EMPTY
If it doesn't, please show the generated SQL.
References
- Hibernate Core Reference Guide
- JPA 1.0 specification
- Section 4.6.11 "Empty Collection Comparison Expressions"
Have you taken a look at your generated SQL? Your method works fine here:
// Hibernate query:const string hql = "from User u where u.Id = 101 and size(u.Appointments) = 0";// Generates this working SQL:select user0_.Id as Id20_, user0_.Name as Name2_20_from User user0_where user0_.Id = 101 and (select count(appointment1_.Id_Solicitud) from Appointment appointment1_ where user0_.Id = appointment1_.Id_User) = 0