问题描述:稿件附件表数据时出现多条重复数据。

介绍

表:稿件实体Manuscripts (数据库表MANUSCRIPTS),稿件附件实体ManuscriptsAtt(表MANUSCRIPTS_ATT),稿件审核实体:ManuscriptsQuotes

表关系:稿件与稿件附件 一对多;稿件与稿件审核一对多;

代码:

稿件实体 Manuscripts 


@Entity
@Table(name = "MANUSCRIPTS")

public class Manuscripts implements Serializable {

//主键
@Id
@Column(length = 38,name = "ID")
private String id; ...... @OneToMany(targetEntity = ManuscriptsAtt.class,fetch=FetchType.EAGER)
@JoinColumn(name="MANUSCRIPTS_ID")
@Fetch(FetchMode.SUBSELECT)
private Set<ManuscriptsAtt> manuscriptsAtts; @OneToMany(targetEntity = ManuscriptsQuotes.class,fetch=FetchType.EAGER)
@JoinColumn(name="MANUSCRIPTS_ID")
@Fetch(FetchMode.SUBSELECT)
private Set<ManuscriptsQuotes> manuscriptsQuotes; ...... }

稿件附件实体:


@Entity
@Table(name = "MANUSCRIPTS_ATT")

public class ManuscriptsAtt implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L; //主键
@Id
@Column(length = 38, name = "ID")
private String id; .....
//父类
@ManyToOne
@JoinColumn(name = "MANUSCRIPTS_ID")
private Manuscripts manuscripts; .....}

稿件审核实体:

@Entity
@Table(name = "MANUSCRIPTS_QUOTES")
public class ManuscriptsQuotes implements Serializable { /**
*
*/
private static final long serialVersionUID = 1L; //主键
@Id
@Column(length = 38,name = "ID")
private String id; //父类
@ManyToOne
@JoinColumn(name = "MANUSCRIPTS_ID")
private Manuscripts manuscripts;
...
}

稿件实体 Manuscripts 中oneToMany 下Fetch设置不同结果不同

@Fetch(FetchMode.JOIN) 会使用left join查询 只产生一条sql语句
@Fetch(FetchMode.SELECT) 会产生N+1条sql语句
@Fetch(FetchMode.SUBSELECT) 产生两条sql语句 第二条语句使用id in (…..)查询出所有关联的数据

下面介绍列出附件数据sql和会出现结果

@Fetch(FetchMode.JOIN) 后sql语句和结果(会使用left join查询 只产生一条sql语句)

Hibernate:
select
this_.id as id1_13_1_,
this_.file_address as file_add6_13_1_,
this_.file_name as file_nam7_13_1_,
this_.manuscripts_id as manuscr11_13_1_,
this_.type as type9_13_1_,
manusc1_.id as id1_11_0_,
manusc1_.content as content4_11_0_,
manusc1_.create_time as create_t5_11_0_,
manusc1_.title as title21_11_0_,
manusc1_.type as type22_11_0_
manuscript4_.manuscripts_id as manuscr11_11_5_,
manuscript4_.id as id1_13_5_,
manuscript4_.id as id1_13_1_,
manuscript4_.file_address as file_add6_13_1_,
manuscript4_.file_name as file_nam7_13_1_,
manuscript4_.manuscripts_id as manuscr11_13_1_,
manuscript4_.type as type9_13_1_,
manuscript5_.manuscripts_id as manuscri7_11_6_,
manuscript5_.id as id1_14_6_,
manuscript5_.id as id1_14_2_,
manuscript5_.department as departme2_14_2_,
manuscript5_.department_name as departme3_14_2_,
manuscript5_.editor_person as editor_p4_14_2_,
manuscript5_.editor_person_name as editor_p5_14_2_,
manuscript5_.manuscripts_id as manuscri7_14_2_,
manuscript5_.quote_time as quote_ti6_14_2_
from
manuscripts_att this_
inner join
manuscripts manusc1_
on this_.manuscripts_id=manusc1_.id
left outer join
manuscripts_att manuscript4_
on manusc1_.id=manuscript4_.manuscripts_id
left outer join
manuscripts_quotes manuscript5_
on manusc1_.id=manuscript5_.manuscripts_id
where
this_.type=?
order by
this_.createtime desc

FetchMode.JOIN  :会使用left join查询 只产生一条sql语句 ,结果数据重复

@Fetch(FetchMode.SELECT) 后sql语句和结果(会产生N+1条sql语句)

Hibernate:
select
this_.id as id1_13_1_,
this_.file_address as file_add6_13_1_,
this_.file_name as file_nam7_13_1_,
this_.manuscripts_id as manuscr11_13_1_,
this_.type as type9_13_1_,
manusc1_.id as id1_11_0_,
manusc1_.content as content4_11_0_,
manusc1_.create_time as create_t5_11_0_,
manusc1_.title as title21_11_0_,
manusc1_.type as type22_11_0_
from
manuscripts_att this_
inner join
manuscripts manusc1_
on this_.manuscripts_id=manusc1_.id
where
this_.type=?
order by
this_.createtime desc
Hibernate:
select
manuscript0_.manuscripts_id as manuscri7_11_0_,
manuscript0_.id as id1_14_0_,
manuscript0_.id as id1_14_1_,
manuscript0_.department as departme2_14_1_,
manuscript0_.department_name as departme3_14_1_,
manuscript0_.editor_person as editor_p4_14_1_,
manuscript0_.editor_person_name as editor_p5_14_1_,
manuscript0_.manuscripts_id as manuscri7_14_1_,
manuscript0_.quote_time as quote_ti6_14_1_
from
manuscripts_quotes manuscript0_
where
manuscript0_.manuscripts_id=?
Hibernate:
select
manuscript0_.manuscripts_id as manuscr11_11_0_,
manuscript0_.id as id1_13_0_,
manuscript0_.id as id1_13_1_,
manuscript0_.createtime as createti2_13_1_,
manuscript0_.create_person as create_p3_13_1_,
manuscript0_.create_person_name as create_p4_13_1_,
manuscript0_.download_count as download5_13_1_,
manuscript0_.file_address as file_add6_13_1_,
manuscript0_.file_name as file_nam7_13_1_,
manuscript0_.file_suff as file_suf8_13_1_,
manuscript0_.manuscripts_id as manuscr11_13_1_,
manuscript0_.type as type9_13_1_,
manuscript0_.view_count as view_co10_13_1_
from
manuscripts_att manuscript0_
where
manuscript0_.manuscripts_id=?
.......................

@Fetch(FetchMode.SELECT) 会产生N+1条sql语句 ,结果正确,但是效率低

@Fetch(FetchMode.SUBSELECT) 产生两条sql语句 第二条语句使用id in (…..)查询出所有关联的数据

Hibernate:
select
this_.id as id1_13_1_,
this_.file_address as file_add6_13_1_,
this_.file_name as file_nam7_13_1_,
this_.manuscripts_id as manuscr11_13_1_,
this_.type as type9_13_1_,
manusc1_.id as id1_11_0_,
manusc1_.content as content4_11_0_,
manusc1_.create_time as create_t5_11_0_,
manusc1_.title as title21_11_0_,
manusc1_.type as type22_11_0_
from
manuscripts_att this_
inner join
manuscripts manusc1_
on this_.manuscripts_id=manusc1_.id
where
this_.type=?
order by
this_.createtime desc
Hibernate:
select
manuscript0_.manuscripts_id as manuscri7_11_1_,
manuscript0_.id as id1_14_1_,
manuscript0_.id as id1_14_0_,
manuscript0_.department as departme2_14_0_,
manuscript0_.department_name as departme3_14_0_,
manuscript0_.editor_person as editor_p4_14_0_,
manuscript0_.editor_person_name as editor_p5_14_0_,
manuscript0_.manuscripts_id as manuscri7_14_0_,
manuscript0_.quote_time as quote_ti6_14_0_
from
manuscripts_quotes manuscript0_
where
manuscript0_.manuscripts_id in (
select
manusc1_.id
from
manuscripts_att this_
inner join
manuscripts manusc1_
on this_.manuscripts_id=manusc1_.id
where
this_.type=?
)

FetchMode.SUBSELECT: 产生两条sql语句 第二条语句使用id in (…..)查询出所有关联的数据 结果正确,效率相对高

hibernate 一对多 取多方数据重复问题,FetchMode.JOIN、FetchMode.SELECT、FetchMode.SUBSELECT区别的更多相关文章

  1. EF 数据重复和缺失问题(select 错误 )

    字段有 id,name,password,sex 1.错误举例: var data = db.User.Select(d => d):   2修正 var data = db.User.Sele ...

  2. 抓取网站数据不再是难事了,Fizzler(So Easy)全能搞定

    首先从标题说起,为啥说抓取网站数据不再难(其实抓取网站数据有一定难度),SO EASY!!!使用Fizzler全搞定,我相信大多数人或公司应该都有抓取别人网站数据的经历,比如说我们博客园每次发表完文章 ...

  3. Hibernate—— 一对多 和 多对多关联关系映射(xml和注解)总结(转载)

    One to Many 映射关系 多对一单向外键关联(XML/Annotation) 一对多单向外键关联(XML/Annotation) 懒加载和积极加载 一对多双向外键关联(XML/Annotati ...

  4. Java进阶知识10 Hibernate一对多_多对一双向关联(Annotation+XML实现)

    本文知识点(目录): 1.Annotation 注解版(只是测试建表)    2.XML版 的实现(只是测试建表)    3.附录(Annotation 注解版CRUD操作)[注解版有个问题:插入值时 ...

  5. Java进阶知识09 Hibernate一对多单向关联(Annotation+XML实现)

    1.Annotation 注解版 1.1.在一的一方加Set 1.2.创建Customer类和Order类 package com.shore.model; import java.util.Hash ...

  6. 11.Hibernate一对多关系

    创建JavaBean 一方: Customer private long cust_id; private String cust_name; private long cust_user_id; p ...

  7. Hibernate之抓取策略

    时间:2017-1-23 19:08 --区分延迟和立即检索1.立即检索    当执行某行代码时,会马上发出SQL语句进行查询.    例如:get()2.延迟检索    当执行某行代码时,不会马上发 ...

  8. Hibernate的检索方式--查询数据的方式

    Hibernate 提供了以下几种检索对象的方式1导航对象图检索方式: 根据已经加载的对象导航到其他对象(根据已经加载的对象,导航到其他对象-例如一对多的查询)2OID 检索方式: 按照对象的 OID ...

  9. Hibernate批量抓取

    ------------------siwuxie095 Hibernate 批量抓取 以客户和联系人为例(一对多) 1.批量抓取 同时查询多个对象的关联对象,是 Hibernate 抓取策略的一种 ...

随机推荐

  1. SSM(Spring + Springmvc + Mybatis)框架面试题

    JAVA SSM框架基础面试题https://blog.csdn.net/qq_39031310/article/details/83050192 SSM(Spring + Springmvc + M ...

  2. C#创建安装、卸载部署程序

    分享3: 需求:对已经开发的应用程序进行安装封装操作,即创建安装.卸载部署程序: 分析:程序的开发是为了在不同的人在不同的机器上使用,为了使不同机器使用该软件就需要见程序安装包,并且保证安装包中必须包 ...

  3. Echarts学习之路2(基本配置项)

    title:标题组件,包含主标题和副标题. title:{ text:"",//主标题 link:"",//主标题文本超链接 target:"&quo ...

  4. Spring boot的第一个demo

    由于SpringBoot的问世使开发的门槛有降低了不少,就其这一点,早已使其名声大振,如雷贯耳.我之前是使用spring开发的,刚才使用了spring boot试验了一下,果然名不虚传,开发速度贼快. ...

  5. android AVD 启动时报错

    AVD启动报错 1.提示:ANDROID_SDK_ROOT is undefined / ERROR: This AVD’s configuration is missing a kernel fil ...

  6. 通过VuePress管理项目文档(一)

    VuePress 相关链接 完整的Vue组件代码以及完整的文档,仅适用于个人参考学习: 文档预览地址:预览链接 使用VuePress编辑文档的代码访问:组件文档 完整代码:组件代码 Vue组件开发 这 ...

  7. Tutorial: Create a Windows Machine Learning UWP application (C#)

    In this tutorial, we'll build a simple Universal Windows Platform application that uses a trained ma ...

  8. System.IO.Pipelines: .NET上高性能IO

    System.IO.Pipelines是一个新的库,旨在简化在.NET中执行高性能IO的过程.它是一个依赖.NET Standard的库,适用于所有.NET实现. Pipelines诞生于.NET C ...

  9. 【ML】从特征分解,奇异值分解到主成分分析

    1.理解特征值,特征向量 一个对角阵\(A\),用它做变换时,自然坐标系的坐标轴不会发生旋转变化,而只会发生伸缩,且伸缩的比例就是\(A\)中对角线对应的数值大小. 对于普通矩阵\(A\)来说,是不是 ...

  10. Android技术文章收集

    Android高工必备技能! 我的 Android 开发实战经验总结 Android开发在路上:少去踩坑,多走捷径 //微信 微信Android客户端架构演进之路 微信Android版智能心跳方案 / ...