I'm relatively green when it comes to Hibernate, but I'm faced with debugging a Criteria query that looks like it should work.
The data structure is roughly as follows:
User { Integer id, String type, String email, List<CustomerPolicy> policies, .... }
CustomerPolicy { List<PolicyTable> tables, ... }
PolicyTable { List<Insured> insureds, ... }
Insured { Applicant applicant, ... }
Applicant { String email, ... }
I am attempting to get a list of emails on all of the Applicants on a User's linked CustomerPolicies, except for those emails that match ones existing in the User table.
Here's the query we have in code, that does not work:
emails.addAll(User.createCriteria().list() { eq("id", id) eq("type", "A") createAlias("policies", "policy") createAlias("policy.tables", "table") createAlias("table.insureds", "insured") createAlias("insured.applicant", "applicant") isNotNull("applicant.email") add(Subqueries.propertyNotIn("applicant.email", emailSubquery)) projections { distinct("applicant.email") } }) emailSubquery = DetachedCriteria.forClass(User).setProjection(Projections.property("email"))
I have a user with around 4 policies, each with around 3-5 tables, each with at least one insured with an applicant, and roughly half of those applicants have an email that is in the User table (alice@company.com) and the other half have emails that aren't in the User table (foo@foo.com).
With the emailSubquery in place, none of the emails are returning. With the Subquery removed, both return. I have verified that foo@foo.com is absolutely not in the User table.
What is wrong with this query?