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

介绍

表:稿件实体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. cocos 动画系统

    前面的话 cocos 动画系统支持任意组件属性和用户自定义属性的驱动,再加上可任意编辑的时间曲线和移动轨迹编辑功能,就可以制作出各种动态效果 概述 Animation 组件可以以动画方式驱动所在节点和 ...

  2. springdata 动态查询之分页

  3. 快速沃尔什变换(FWT)及K进制异或卷积&快速子集变换(FST)讲解

    前言: $FWT$是用来处理位运算(异或.与.或)卷积的一种变换.位运算卷积是什么?形如$f[i]=\sum\limits_{j\oplus k==i}^{ }g[j]*h[k]$的卷积形式(其中$\ ...

  4. Python神器 Jupyter Notebook

    什么是Jupyter Notebook? 简介 Jupyter Notebook是基于网页的用于交互计算的应用程序.其可被应用于全过程计算:开发.文档编写.运行代码和展示结果. Jupyter Not ...

  5. TOP按钮

    TOP按钮 博客园页面添加返回顶部TOP按钮 进入网页管理->设置 在"页面定制CSS代码"中添加如下css样式,当然你可以改为自己喜欢的样式 此处可以将背景色backgro ...

  6. docker添加阿里云专属镜像

    阿里云镜像地址:https://link.zhihu.com/?target=https%3A//cr.console.aliyun.com/%23/accelerator 根据提示开启容器镜像服务, ...

  7. RTC及sensor时间同步

    https://blog.csdn.net/dai_jing/article/details/38147419 ----------------------------- linux 的系统时间有时跟 ...

  8. QString与LPWSTR之间的转换;

    QString 转换成 LPWSTR LPWSTR lpStr = (LPWSTR) QString("nihao").toStdWString().c_str();

  9. springboot集成freemarker静态资源无法访问

    如题配置文件加上 #设定静态文件路径,js,css等.static为你放置静态资源的文件夹名称,也可以叫别的名字.properties加上 spring.mvc.static-path-pattern ...

  10. IScroll5不能滑到最底端的解决办法

    IScroll总体上用起来比较简单,但是如果用不好的可能会产生底部一点滚动不上去的问题. 环境:weui+iscroll5 整体布局及id如下 searchbarwrapper   divscroll ...