yon*_*s88 6 java hibernate hql
我有以下代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
它引发了一个例外:
org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list
Run Code Online (Sandbox Code Playgroud)
结果我想得到的只是代码.
JB *_*zet 22
join fetch val.classDAO.b意思是"在提取时val,也可以获取classDAO链接到val".但是您的查询无法获取val.它只取val.code.所以抓取没有任何意义.只需删除它,一切都会好起来的:
select distinct val.code from ValueDAO val
left join val.classDAO classDAO
where classDAO.id = ?
order by val.code
Run Code Online (Sandbox Code Playgroud)
但有些说明:
classDAO.id = ?意味着连接实际上是一个内连接(因为classDAO不能为null并且同时具有给定的ID)鉴于上述情况,可以将查询重写为
select distinct val.code from ValueDAO val
where val.classDAO.id = ?
order by val.code
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29186 次 |
| 最近记录: |