-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: OneToMany Annotations Map<X,V> query question.
PostPosted: Sat Apr 07, 2012 10:32 am 
Newbie

Joined: Sat Oct 23, 2010 2:08 am
Posts: 1
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.
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.