hibernate left join fetch 出错的问题
1.首先说说manyToOne的问题
比如一个用户所在的组织机构,可能是多个,最多是四个,然后userEntity有下的代码:

关联查询:
第一种方式:代码如下
StringBuilder sql = new StringBuilder();
sql.append("select a.zdbh as zdbh, a.username as username, a.xm as xm,a.yhzt as yhzt, ")
.append(" a. jg1.zzjgid as jgid1, a. jg2.zzjgid as jgid2, a. jg3.zzjgid as jgid3, a. jg4.zzjgid as jgid4, ")
.append(" a. jg1.jgmc as jgmc1, a. jg2.jgmc as jgmc2, a. jg3.jgmc as jgmc3, a. jg4.jgmc as jgmc4 ")
.append(" from IppcXtglYhxxEntity as a ")
上面查询的问题是,会丢失某些用户,因为机构4个字段,部分会是空,通过show sql,可以看到,转换的SQL是等值连接查询:
select
ippcxtglyh0_.zdbh as col_0_0_,
ippcxtglyh0_.username as col_1_0_,
ippcxtglyh0_.xm as col_2_0_,
ippcxtglyh0_.yhzt as col_4_0_,
ippcxtglyh0_.jgid1 as col_5_0_,
ippcxtglyh0_.jgid2 as col_6_0_,
ippcxtglyh0_.jgid3 as col_7_0_,
ippcxtglyh0_.jgid4 as col_8_0_,
ippcxtglzz2_.jgmc as col_9_0_,
ippcxtglzz3_.jgmc as col_10_0_,
ippcxtglzz4_.jgmc as col_11_0_,
ippcxtglzz5_.jgmc as col_12_0_
from
ippc_xtgl_yhxx ippcxtglyh0_,
ippc_xtgl_zzjg ippcxtglzz2_,
ippc_xtgl_zzjg ippcxtglzz3_,
ippc_xtgl_zzjg ippcxtglzz4_,
ippc_xtgl_zzjg ippcxtglzz5_
where
ippcxtglyh0_.jgid1=ippcxtglzz2_.zdbh
and ippcxtglyh0_.jgid2=ippcxtglzz3_.zdbh
and ippcxtglyh0_.jgid3=ippcxtglzz4_.zdbh
and ippcxtglyh0_.jgid4=ippcxtglzz5_.zdbh
and ippcxtglyh0_.bhzxid=20
修改后,方案二:通过left join,不会丢失数据

上述SQL:转换后的原生态的Sql如下:
select
ippcxtglyh0_.zdbh as col_0_0_,
ippcxtglyh0_.username as col_1_0_,
ippcxtglyh0_.xm as col_2_0_,
ippcgybhzx5_.mc as col_3_0_,
ippcxtglyh0_.yhzt as col_4_0_,
ippcxtglzz1_.zdbh as col_5_0_,
ippcxtglzz2_.zdbh as col_6_0_,
ippcxtglzz3_.zdbh as col_7_0_,
ippcxtglzz4_.zdbh as col_8_0_,
ippcxtglzz1_.jgmc as col_9_0_,
ippcxtglzz2_.jgmc as col_10_0_,
ippcxtglzz3_.jgmc as col_11_0_,
ippcxtglzz4_.jgmc as col_12_0_
from
ippc_xtgl_yhxx ippcxtglyh0_
left outer join
ippc_xtgl_zzjg ippcxtglzz1_
on ippcxtglyh0_.jgid1=ippcxtglzz1_.zdbh
left outer join
ippc_xtgl_zzjg ippcxtglzz2_
on ippcxtglyh0_.jgid2=ippcxtglzz2_.zdbh
left outer join
ippc_xtgl_zzjg ippcxtglzz3_
on ippcxtglyh0_.jgid3=ippcxtglzz3_.zdbh
left outer join
ippc_xtgl_zzjg ippcxtglzz4_
on ippcxtglyh0_.jgid4=ippcxtglzz4_.zdbh cross
join
ippc_gy_bhzx ippcgybhzx5_
where
ippcxtglyh0_.bhzxid=ippcgybhzx5_.zdbh
and ippcxtglyh0_.bhzxid=20
方案二是修改后的SQL,本来写的SQL中加入了fetch,即left join fetch a.jg1 as jg1
遇到的问题是:
query specified join fetching, but the owner of the fetched association was not present in the select list
代码
原因分析:如果使用了fetch,拥有者一定要出现在select中,也就是上面的IppcXtglYhxxEntity as a, a必须出现在select语句中
例如将上面改为select a... 这样就会执行正常,
因为使用了fetch,Hibernate就会将需要fetch的对象(jd1、jg2、jg3、jg4)立即加载在父对象(IppcXtglYhxxEntity )中
,而我的select拥有者(IppcXtglYhxxEntity )并没有present(出席在结果集中),那么就会出现以上错误.
解决办法:将fetch去掉即可 或者 修改select,改成select a,....
我采用的是去掉fetch的方法。
个人项目遇到问题,如需转载,请注明出处!!!
hibernate left join fetch 出错的问题的更多相关文章
- Hibernate逍遥游记-第7章 Hibernate的检索策略和检索方式(<set lazy="false" fetch="join">、left join fetch、FetchMode.JOIN、)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate中,left join、inner join以及left join fetch区别(转)
标签: hibernate hql inner join left right 杂谈 分类: SQL 原文地址:http://m33707.iteye.com/blog/829725 Select F ...
- [Hibernate] inner Join和 left Join
@Test public void test11(){ Session ss=HibernateUtil.getSession(); //根据员工名称(SCOTT)找到和他所在的部门的其他员工的信息 ...
- Hibernate中得fetch
fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例:fetch = "select"是 ...
- HQL语句中的join fetch
from Paper as paper join fetch paper.authors as authors where authors.id='xxxx'; from Paper as paper ...
- hibernate中使用fetch来决策性能方案
什么时候用子查询,或者连接查询 一般多个数据的对应用子查询,单一行的数据用连接 (若要查询每个学生分别学了什么课程 ,若要fetch=join.fetch=select) 则是这种情况 Hiberna ...
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-006Mixing inheritance strategies(@SecondaryTable、@PrimaryKeyJoinColumn、<join fetch="select">)
一.结构 For example, you can map a class hierarchy to a single table, but, for a particular subclass, s ...
- Hibernate left join
6.4.5 左外连接 左外连接(Left Outer Join)查询出左表对应的复合条件的所有记录,如查询李晓梅同学的选课信息.下面是类HQLLeftOuterJoinQuery的源代码. 其实关联 ...
- Hibernate 自动更新表出错 More than one table found in namespace
报错:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table f ...
随机推荐
- 合肥工业大学oj p1012
#include <iostream> #include <cstring> #define maxn 5 + 2 using namespace std; int m, n, ...
- Neural Network Programming - Deep Learning with PyTorch with deeplizard.
PyTorch Prerequisites - Syllabus for Neural Network Programming Series PyTorch先决条件 - 神经网络编程系列教学大纲 每个 ...
- java如何快速创建List
几个快速添加list的方法 1. 使用Collections.addAll()方法,前提还是需要手动 new ArrayList ArrayList<String> s = new Arr ...
- Day04_网络爬虫图片收获
#所需模块 requests .Beautifulsoup.urllib 1. response = requests.get('www.baidu.com') #获取网站响应 2.html = r ...
- Could not find method google() for arguments [] on repository container,rn 集成react-native-camera 出现此错误的解决方法
(1) app/build.gradle android { compileSdkVersion buildToolsVersion "25.0.2" } compile (pro ...
- 炸金花游戏(4)--炸金花AI基准测试评估
前言: 本文将谈谈如何评估测试炸金花的AI, 其实这个也代表一类的问题, 德州扑克也是类似的解法. 本文将谈谈两种思路, 一种是基于基准AI对抗评估, 另一种是基于测试集(人工选定牌谱). 由于炸金花 ...
- 在typeScript+vue项目中使用ref
因为vue项目是无法直接操作dom的,但是有时候开发需求迫使我们去操作dom. 两个办法,一个是很low的再引入jq,然后通过jq来操作,但是这样就失去了我们使用vue的意义, 可惜的是我曾经这样干过 ...
- JS拖拽div(移动)
<!doctype html><html><head> <meta charset="utf-8"> <title>JS ...
- Scrum学习心得
一.Scrum学习心得: 最近简单的学习了一下scrum模式,感觉又开启了一个新世界的大门. 首先,scrum是一个应用于互联网研发的开发方式,这种开发方式的主要特点是快速迭代,持续交付. scrum ...
- java根据ip地址获取详细地域信息的方法
通过淘宝IP地址库获取IP位置(也可以使用新浪的) 请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串] 响应信息:(jso ...