Hello Having trouble setting up a query using criteria with the follow annotations/classes. It generates the proper schema in the database I just can't figure out how to write the query using criteria. Hibernate is generating the correct schema for the tables.
Hibernate Version: 4.1.0.Final
Query in SQL:
SELECT * FROM PARENT WHERE ID IN (SELECT DISTINCT(PARENT_ID) From PARENT_CHILD WHERE child_id IN (....));
Resulting Tables:
TABLE PARENT
ID|...other fields|
TABLE PARENT_CHILD
PARENT_ID|CHILD_D|EmbeddableMapKey Fields|
TABLE CHILD
ID|...other fields|
Here is what I currently Have:
Code:
public DetachedCriteria createCriteria(List<Child> children, ...) {
// Criteria creation....
DetachedCriteria query = DetachedCriteria.forClass(Parent.class, "Parent");
query.createAlias("Parent.children", "child");
DetachedCriteria subquery = DetachedCriteria.forClass(Parent.class, "Parent")
subquery.createAlias("Parent.children", "child");
subquery.add(Restrictions.in("child", children));
subquery.setProjection(Projections.distinct(Projections.property("id"));
query.add(Subqueries.propertyIn("id", subquery));
return query;
}
// Yes I know, this function isn't actually in the code.
public <T> List<T> execute(DetachedCriteria query) {
Session session = grabSession();
return (List<T>) query.getExecutableCriteria(session).list();
}
When the query is run I get the following error:
java.lang.NullPointerException: null at org.hibernate.loader.criteria.CriteriaQueryTranslator.getEntityName(CriteriaQueryTranslator.java:450)
Classes with Annotations:
Code:
@Entity
public class Parent {
Long id;
Map<EmbeddableKey, Child> children;
// Other Fields..
@Id
public Long getId() { return id; }
@OneToMany(targetEntity = Child.class)
@MapKeyClass(EmbeddableMapKey.class)
public Map<EmbeddableMapKey, Child> getChildren() { return children; }
}
@Entity
public class Child {
Long id;
// Other Fields..
@id
public Long getId() { return id; }
}
@Embeddable
public class EmbeddableMapKey {
// Other data fields, mostly flags.
}