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 ...
随机推荐
- 更改Windows更新源(解决公司内部网络无法下载语言包或更新的问题)
打开注册表 找到HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate 将WUServer.WUStatusServe ...
- 开始Python学习
主要结合ArcGIS进行空间数据处理 Python最讨厌的就是版本问题了 ArcGIS 10.5安装的时候已经安装了python2.7.13,但后来又安装了python3.6.1. 环境变量的设置: ...
- 转 Mac 下自带的中文输入法不显示汉字提示问题
原文 https://blog.csdn.net/moxi_wang/article/details/50721326 当时聊天的时候不知道那个手指头按错了什么键 导致Mac自带的中文输入法不能提示显 ...
- window10单机安装storm集群
适合范围:storm自由开源的分布式实时计算系统,擅长处理海量数据.适合处理实时数据而不是批处理. 安装前的准备 1.安装zookeeper ①下载zookeeperhttps://zookeeper ...
- Image Widget 的几种加入形式
image .asset : 加载资源图片,会使打包时包体过大 image.network :网络资源图片,经常换的或者动态的图片 image file : 本地图片,比如相册 重用属性: fit ...
- 浅谈get,post,put和delete请求
get.put.post.delete含义与区别 1.GET请求会向数据库发索取数据的请求,从而来获取信息,该请求就像数据库的select操作一样,只是用来查询一下数据,不会修改.增加数据,不会影 ...
- shell中的函数、数组、报警系统脚本
1.shell中的函数 函数就是把一段代码整理到了一个小单元中,并给这个小单元起一个名字,当用到这段代码时直接调用这 个小单元的名字即可.格式: function f_name() {commond} ...
- 作业二:构建swap函数
一.swap代码 #include<stdio.h> int main() //主函数部分 { void swap(int *m,int *n); int a,b; int *p1,*p2 ...
- 18.17 U-Boot+内核移植
18.17.1 移植U-Boot-2012.04.08 1.下载.建立source insight工程.编译.烧写.如果无运行分析原因. $ .tar.bz2 $ cd u-boot- $ make ...
- Nginx 配置location root 转自https://blog.csdn.net/rofth/article/details/78581617
nginx指定文件路径有两种方式root和alias,root与alias主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上. 最基本的区别 ...