problem with "select new Object ... join ... where"

Posted by jacob on Stack Overflow See other posts from Stack Overflow or by jacob
Published on 2010-06-09T14:26:04Z Indexed on 2010/06/09 16:42 UTC
Read the original article Hit count: 184

Filed under:
|
|

Hi, I'm having a problem with an HQL query

Three classes

ClassOne is my BusinessObject

public class ClassOne {  
  private int id;  
  private int status;
  private Set<ClassTwo> classTwos;  
  + other fields/getters/setters/constructor etc  
}

ClassTwo is referenced in a set of ClassOne and is kind of the history of an object of ClassOne

public class ClassTwo {  
  private int id;  
  private int oldStatus;  
  private int newStatus;  
  private String message;  
  //+ getters/setters/constructor etc  
}

ClassThree is my DTO/VO with just one classTwo (not the whole history)

public class ClassThree {  
  private int id;  
  private int status;  
  private ClassTwo classTwo;  
  public ClassThree(int pId, int pStatus, ClassTwo pClassTwo) {  
    id=pId;  
    status=pStatus;  
    classTwo=pClassTwo;   
  }
  //+ getters/setters etc
}

Now I'd like to create an HQL query like this:
I'd like to get all objects of ClassThree with a certain status and if it exists the newest ClassTwo with a certain newStatus.
For example:
I'd like to get all the DTOs (ClassThree) of ClassOne whose status is now 1, but earlier in their history it has been 2 and I'd like to have the latest ClassTwo object which has 2 as newStatus.

SELECT new ClassThree(c1.id, c1.status, c2)  
FROM ClassOne c1  
LEFT JOIN c1.classtwos c2 (...)

and (...) is where I don't know what to do, I'm not even sure if it's a join / join fetch

Looked around and tried quite a lot already, but no clue. Especially with the join fetch I get some Hibernate errors like org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list.

Fetching the BusinessObject like that is no problem

SELECT distinct(c1)
FROM ClassOne c1  
LEFT OUTER JOIN FETCH c1.classtwos c2  

and I get the ClassTwos as my field.

Thanks in advance,
Jacob

P.S.: One thing might be important, ClassTwo has no reference to ClassOne!!

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate