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 ...
随机推荐
- svg的使用集合
1.效果 https://www.zhangxinxu.com/wordpress/2017/03/offset-path-css-animation/ 2.用法 https://www.cnblog ...
- CSS盒子内容
内边距 内边距(padding):内容与边框之间的距离 注意:padding 只能移动盒子的内容 padding属性联写: padding: 10px: 内边距的上下左右都移动 padding: 10 ...
- linux服务器共享给windows的client打印机配置
最近实验室新进来一台服务器还有打印机,老大意思让服务器连接打印机并进行网络共享,其他的人可以通过自己的PC连接到共享打印机,打印各自电脑的文件.这样的需求可能很多人都有遇到,我也是遇到这件事,在网上搜 ...
- JavaScript中常用的BOM对象(属性、方法)
window对象 定义: 一个浏览器窗口实例 与窗口有关的信息(应用程序编程接口) ECMAScript规定的Global对象 方法 open(url),返回标识符 引用 即将打开窗口的.(调用该引用 ...
- iOS Masonry控件等比例布局
一.先解释相关API 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /** * distribute with fixed spacing * * ...
- Js/Session和Cookies的区别
1.cookies数据存放在客户的浏览器上面,session放在服务器上面.2.cookies不安全,别人可以分析浏览器的数据进行cookies的欺骗,考虑到安全性,应该使用cookie3.sessi ...
- linux下centos解压时报错: gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
最近在linux下安装python时,解压Python.tgz文件时遇到一个问题: gzip: stdin: not in gzip format tar: Child r ...
- Linux CPU瓶颈问题分析
虚线部分为`下一步`的分析方向,图是网络抄的,放这里更容易找
- 中文dumps显示
json.dumps(tuwen_attention_dict_set, ensure_ascii=False)
- mysql_索引
.默认情况下大多使用Btree索引,该索引就是通常所见 唯一索引.聚簇索引等等,Btree用在OLTP,加快查询速度 查询表索引 show index from tablename 查询表结构 ...