加入提取:“查询指定的连接提取,但获取的关联的所有者不在选择列表中” [英] Join fetch: "query specified join fetching, but the owner of the fetched association was not present in the select list"

查看:171
本文介绍了加入提取:“查询指定的连接提取,但获取的关联的所有者不在选择列表中”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

  public class ValueDAO实现了BusinessObject< Long> {

私人长ID;
私人字符串代码;
私人ClassDAO classDAO;
....
}

public List< String> getCodesByCodeClass(Long classId){
String select =从ValueDAO val中选择不同的val.code val+
join fetch val.classDAO;
String where =where val.classDAO.id =?order by val.code;

return getHibernateTemplate()。find(select + where,classId);
}

引发一个例外:

  org.hibernate.QueryException:查询指定的连接抓取,但抓取的关联的所有者不在选择列表中

在结果中,我仅获取代码。 p> join fetch val.classDAO.b 表示当获取 val 时,也会获取 classDAO 链接到 val 。但是您的查询不会获取 val 。它仅提取 val.code 。所以取回没有意义。只要删除它,一切都会好的:

 从ValueDAO中选择不同的val.code val 
left join val。 classDAO classDAO
其中classDAO.id =?
order by val.code

尽管如此:


  • 进行左连接,然后添加像 classDAO.id =?这样的限制意味着连接实际上一个内部联接(因为classDAO不能为空,并且同时具有给定的ID) 命名您的实体的命令
  • XxxDAO非常混乱。 DAO和实体根本不是一回事。



鉴于上述情况,查询可以改写为

 从ValueDAO中选择不同的val.code val 
其中val.classDAO.id =?
order by val.code


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 fetching val, also fetch the classDAO linked to the val". But your query doesn't fetch val. It fetches val.code only. So the fetch makes no sense. Just remove it, and everything will be fine:

select distinct val.code from ValueDAO val 
left join val.classDAO classDAO
where classDAO.id = ? 
order by val.code

Some notes, though:

  • doing a left join and then adding a retriction like 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)
  • naming your entities XxxDAO is very confusing. DAOs and entities are not the same thing at all.

Given the above, the query can be rewritten as

select distinct val.code from ValueDAO val 
where val.classDAO.id = ? 
order by val.code

这篇关于加入提取:“查询指定的连接提取,但获取的关联的所有者不在选择列表中”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆