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 出错的问题的更多相关文章

  1. Hibernate逍遥游记-第7章 Hibernate的检索策略和检索方式(<set lazy="false" fetch="join">、left join fetch、FetchMode.JOIN、)

    1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...

  2. Hibernate中,left join、inner join以及left join fetch区别(转)

    标签: hibernate hql inner join left right 杂谈 分类: SQL 原文地址:http://m33707.iteye.com/blog/829725 Select F ...

  3. [Hibernate] inner Join和 left Join

    @Test public void test11(){ Session ss=HibernateUtil.getSession(); //根据员工名称(SCOTT)找到和他所在的部门的其他员工的信息 ...

  4. Hibernate中得fetch

    fetch ,可以设置fetch = "select" 和 fetch = "join" 用一对多来举例:fetch = "select"是 ...

  5. HQL语句中的join fetch

    from Paper as paper join fetch paper.authors as authors where authors.id='xxxx'; from Paper as paper ...

  6. hibernate中使用fetch来决策性能方案

    什么时候用子查询,或者连接查询 一般多个数据的对应用子查询,单一行的数据用连接 (若要查询每个学生分别学了什么课程 ,若要fetch=join.fetch=select) 则是这种情况 Hiberna ...

  7. 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 ...

  8. Hibernate left join

    6.4.5  左外连接 左外连接(Left Outer Join)查询出左表对应的复合条件的所有记录,如查询李晓梅同学的选课信息.下面是类HQLLeftOuterJoinQuery的源代码. 其实关联 ...

  9. Hibernate 自动更新表出错 More than one table found in namespace

    报错:Caused by: org.hibernate.tool.schema.extract.spi.SchemaExtractionException: More than one table f ...

随机推荐

  1. SharePoint 2013 解惑 无法打开文件浏览器

    你有时候会看到这东西谈出来,当你想像管理文件一样,管理SharePoint上资源的时候 意思是说,不能打开文件浏览器,请加入你的站点到信任站点,这个有两个问题,一个是IE设置,一个是WebClient ...

  2. LSC问题(不连续问题)

    转载:http://blog.csdn.net/v_JULY_v/article/details/6110269 本文是动态规划算法中,网上写得最好的一个之一.看完很容易理解.需要重点理解的部分,我会 ...

  3. 连续多次调用inet_ntoa()结果重复

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pcap.h> ...

  4. 尝鲜svnup

    最近有同事折腾了一下svnup的编译,终于可以在Mac OS X和Linux上面编译通过了,仓库在这里:https://github.com/lvzixun/svnup/ svnup这个工具只有一个功 ...

  5. C# 树状图

    效果图: 结构: frmMain层 using hierarchy.BLL; using hierarchy.Model; using System; using System.Collections ...

  6. python logging 日志轮转文件不删除问题的解决方法

    项目使用了 logging 的 TimedRotatingFileHandler : #!/user/bin/env python # -*- coding: utf-8 -*- import log ...

  7. JVM垃圾收集器-Parallel Scavenge收集器

    今天我给大家讲讲JVM垃圾收集器-Parallel Scavenge收集器 Parallel Scavenge收集器 Parallel Scavenge收集器也是一个新生代收集器,它也是使用复制算法的 ...

  8. ASP.NET+MVC+EntityFramework快速实现增删改查

    本教程已经录制视频,欢迎大家观看我在CSDN学院录制的课程:http://edu.csdn.net/lecturer/944

  9. PTA8

    这个作业属于哪个课程 C语言程序设计2 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/software-engineering-class2-2018/ ...

  10. OSI七层网络模型浅析

    OSI七层网络模型(从下往上): 物理层(Physical):设备之间的数据通信提供传输媒体及互连设备,为数据传输提供可靠的 环境.可以理解为网络传输的物理媒体部分,比如网卡,网线,集线器,中继器,调 ...