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

介绍

表:稿件实体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. Django3 Django 路由分发,反向解析,2.0版本的path

    urls配置像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码对应执行. 1.简 ...

  2. Linux(Ubuntu)使用日记------部署JavaWeb项目到服务器

    0.前言 本博文内容是建立在你可以通过SSH连接到远程服务器的基础上的,如果你还没有用SSH连接到远程服务器,请参考此文(腾讯云服务器): http://www.cnblogs.com/hwtblog ...

  3. centos7之zabbix邮件报警(短信报警)

    前言 前面我们介绍了zabbix的基本linux和window及SNMP流量的简单监控,我们知道作为运维人员,需要7x24小时待命,但是我们不可能时时刻刻都坐在电脑旁边查看监控上的各个主机状态,所以我 ...

  4. 定时任务调度工作(学习记录 三)timer其他重要函数

    TimerTask的两个重要函数: 1.cancel() 作用: 取消当前TimerTask里的任务 演示: 先在继承了TimerTask的类中添加一个计时器,然后在run方法中合适的位置添加canc ...

  5. 了解Vue.js

    一.了解Vue (1)Vue.js在设计上采用MVVM(Model-View-ViewModel)模式 当View变化时,会自动更新到ViewModel,反之亦然.View与ViewModel通过双向 ...

  6. Adding appsettings.json to a .NET Core console app

    This is something that strangely doesn’t seem to be that well documented and took me a while to figu ...

  7. NSParagraphStyle 的属性

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 20)]; label.font = [UIFont sys ...

  8. Centos 6.5下mysql 8.0.11的rpm包的安装方式

    1.系统版本及mysql下载地址 操作系统:Centos 6.5(Centos 7.4下载对应的mysql版本安装同理) mysql数据库版本:mysql8.0.11 mysql官方网站:http:/ ...

  9. leanote 信息栏显示笔记本和笔记类型

    本文解决如下两个问题: 1. 在列表视图下使用搜索时,不知道搜出来的笔记属于哪个笔记本.(摘要视图下是有显示的) 2. 增加显示笔记类型(markdown 或 富文本) 修改resources\app ...

  10. [powershell]获取FCID&Port

    Get-InitiatorID Get-InitiatorPort