I have a following code:
public class ValueDAO implements BusinessObject<Long> {
private Long id;
private String code;
private ClassDAO classDAO ;
....
}
public List<String> getCodesByCodeClass(Long classId) {
String select = "select distinct val.code from ValueDAO val left " +
"join fetch val.classDAO ";
String where = "where val.classDAO.id = ? order by val.code";
return getHibernateTemplate().find(select + where, classId);
}
It raises an exception:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
In the result I wan to get only codes.
join fetch val.classDAO.b
means "when fetchingval
, also fetch theclassDAO
linked to theval
". But your query doesn't fetchval
. It fetchesval.code
only. So the fetch makes no sense. Just remove it, and everything will be fine:Some notes, though:
classDAO.id = ?
means that the join is in fact an inner join (since classDAO can't be null and have the given ID at the same time)Given the above, the query can be rewritten as