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

介绍

表:稿件实体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. 滴滴 CTO 架构师 业务 技术 战役 时间 赛跑 超前 设计

    滴滴打车CTO张博:生死战役,技术和时间赛跑-CSDN.NEThttps://www.csdn.net/article/2015-06-25/2825058-didi 滴滴出行首席架构师李令辉:业务的 ...

  2. Python Scrapy项目创建(基础普及篇)

    在使用Scrapy开发爬虫时,通常需要创建一个Scrapy项目.通过如下命令即可创建 Scrapy 项目: scrapy startproject ZhipinSpider 在上面命令中,scrapy ...

  3. jstl的foreach标签

    jsp支持丰富的jstl标签语言(需要jar包支持),其中list循环(迭代)用的是<c:forEach></c:forEach>标签. 这个标签的作用就是迭代输出标签内部的内 ...

  4. nginx的概念与几种负载均衡算法

    Nginx的背景 Nginx和Apache一样都是一种WEB服务器.基于REST架构风格,以URI(Uniform Resources Identifier,统一资源描述符)或URL(Uniform ...

  5. react-native-echarts在打包时出现的坑

    react-native-echarts目前是RN开发中使用echarts图表最好的插件了,用法与Echarts完全一致,默认提供了三个属性: option (object): The option ...

  6. jQuery 源码学习 - 01 - 简洁的 $('...')

    首先贴上学习参考资料:[深入浅出jQuery]源码浅析--整体架构,备用地址:chokcoco/jQuery-. jQuery 库,js 开发的一个里程碑,它的出现,让网页开发者们告别荒蛮的上古时代, ...

  7. springboot 配置mybatis

  8. 源码分析 ucosii/source 任务源码详细分析

    分析源码: 得先学会读文档, 函数前边的 note :是了解该程序员的思想的途径.不得不重视 代码前边的  Notes,了解思想后,然后在分析代码时看他是如何具体实现的. 1. ucosii/sour ...

  9. 《java核心技术36讲》学习笔记-------杨晓峰(极客时间)

    非常荣幸作为晓峰哥的同事,之前就看过这篇文章,重写读一遍,再学习学习. 一.开篇词 初级.中级:java和计算机科学基础.开源框架的使用:高级.专家:java io/nio.并发.虚拟机.底层源码.分 ...

  10. docker基本使用

    1.启动执行一次的容器 2.启动交互式容器 -i:告诉docker守护进程始终打开交互输入 -t:给容器分配一个伪tty终端 3.查看容器 docker ps:查看正在运行的容器 docker ps ...