在说解决cannot simultaneously fetch multiple bags异常之前,我先说下抓取策略
注解@Fetch(FetchMode.?)抓取策略有三种
1、FetchMode.JOIN(默认的抓取策略),采用外连接的形式,left outer join ... on
2、FetchMode.SELECT 会另外发送一条sql语句加载当前对象的关联实体
3、FetchMode.SUBSELECT 会另外发送一条select语句抓取前面查询到的所有实体对象的关联实体
通过Hibernate输出的SQL日志看成,个人感觉2、3的差别不是太大 ,都是另起select语句查询与当前某个实体相关联的其他实体。
下面是我写的一个简单DEMO,主要来验证抓取策略和异常的处理方法
表结构
人员表 tb_person
+-------------+
| Field |
+-------------+
| person_id |
| person_age |
| person_name |
+-------------+
一个人员可以有多个邮箱,邮箱表,人-邮箱是一对多的关系tb_person_email
+-----------+
| Field |
+-----------+
| id |
| person_id |
| address |
+-----------+
事件表,一个人可以有多个事件,一个事件也可有多个人处理,是多对多关系tb_event,tb_person_event
+-------------+
| Field |
+-------------+
| event_id |
| event_title |
| event_date |
+-------------+
+-----------+
| Field |
+-----------+
| event_id |
| person_id |
+-----------+
要执行的方法是:搜索人,以及相关的事件和邮件。
person.java
第一种写法,采用默认join抓取策略,进行强制抓取person所有关联对象的方式
- @ManyToMany(fetch=FetchType.EAGER)
- @JoinTable(name = "TB_PERSON_EVENT", joinColumns = {@JoinColumn(name = "PERSON_ID")},
- inverseJoinColumns = {@JoinColumn(name = "EVENT_ID")})
- public List<Event> getEvents() {
- return events;
- }
- @OneToMany(fetch=FetchType.EAGER)
- @JoinTable(name = "TB_PERSON_EMAIL", joinColumns = {@JoinColumn(name = "PERSON_ID")},
- inverseJoinColumns = {@JoinColumn(name = "ID")})
- ublic List<Email> getEmails() {
- return emails;
如果这样写的情况下, 就会报cannot simultaneously fetch multiple bags异常,从SQL日志分析,hibernate是这样执行的
- select *
- from TB_PERSON tp left outer join TB_PERSON_EMAIL tpe1 on tp.person_id = tpe1.person_id
- left outer join TB_PERSON_EMAIL tpe2 on tpe1.id = tpe2.id
- left outer join TB_PERSON_EVENT tpet on tp.person_id = tpet.person_id
- left outer join TB_EVENT te on tpet.event_id = te.event_id
- where tp.person_id = 2;
输出的记过,就会出现两个同样的email实体列,所以就报上述异常。
如果要避免这个异常,我在网上搜了许多资料都是说把fetch=FetchType.LAZY
变成懒加载模式,或者把List修改成Set集合,这两种我验证过了,是可以行的通的,但是如果不想采取这种两种办法,
怎么办。
其实还有一种,是看springside得到的一种方法,代码修改如下
- @ManyToMany(fetch=FetchType.EAGER)
- @JoinTable(name = "TB_PERSON_EVENT", joinColumns = {@JoinColumn(name = "PERSON_ID")},
- inverseJoinColumns = {@JoinColumn(name = "EVENT_ID")})
- @Fetch(FetchMode.SUBSELECT)
- public List<Event> getEvents() {
- return events;
- }
- @OneToMany(fetch=FetchType.EAGER)
- @JoinTable(name = "TB_PERSON_EMAIL", joinColumns = {@JoinColumn(name = "PERSON_ID")},
- inverseJoinColumns = {@JoinColumn(name = "ID")})
- @Fetch(FetchMode.SUBSELECT)
- public List<Email> getEmails() {
- return emails;
- }
这种方法就是在不修改原结构注释的情况下,可以修改一下抓取策略,分开select实体,这样就不会出现重复列现象。
个人感觉还是不错的方法,打算就用它了。
另外就是个人并不是太喜欢全抓取,密密麻麻一大堆的SQL打印的控制台上,排查SQL都麻烦,所以最终修改版,个人比较喜欢的注释方法:
- @ManyToMany(fetch=FetchType.LAZY)
- @JoinTable(name = "TB_PERSON_EVENT", joinColumns = {@JoinColumn(name = "PERSON_ID")},
- inverseJoinColumns = {@JoinColumn(name = "EVENT_ID")})
- @Fetch(FetchMode.SUBSELECT)
- public List<Event> getEvents() {
- return events;
- }
- @OneToMany(fetch=FetchType.LAZY)
- @JoinTable(name = "TB_PERSON_EMAIL", joinColumns = {@JoinColumn(name = "PERSON_ID")},
- inverseJoinColumns = {@JoinColumn(name = "ID")})
- @Fetch(FetchMode.SUBSELECT)
- public List<Email> getEmails() {
- return emails;
- }
转自:http://dyldragon.iteye.com/blog/788385
相关推荐
Differential absorption lidar (DIAL) has ... In this paper, a simple and accurate algorithm is presented for simultaneously detecting and estimating multiple vapor materials with multiple-wavelength DI
We wish to make sure that a company cannot effectively restrict the users of a free program by obtaining a restrictive license from a patent holder. Therefore, we insist that any patent license ...
Simultaneously toughening and reinforcing high-density polyethylene
Modeling and Simultaneously Removing Bias via Adversarial Neural Networks.pdf 位置偏置
The electrodynamics ofsubstances with simultaneously negative values of ε and µ
Charging utility maximization in wireless rechargeable sensor networks by charging multiple sensors simultaneously
This paper combines workspace models with optimization techniques to simultaneously address wholearm collision avoidance, joint limits and camera field of view (FOV) limits for vision-based motion ...
A three-dimensional inverse geometry problem in estimating simultaneously
Simultaneously achieving self-toughening and self-reinforcing of polyethylene
View Assessment Result: Multiple-Choice Quiz 2 Your performance was as follows: 1. The degree of a table is the number of _____ in the table. (a) keys (b) columns (c) rows (d) ...
may be associated with multiple labels simultaneously. A key difference between multi-label learning and traditional binary or multi-class learning is that the labels in multi-label learning are not ...
TEmailServer is a built-in SMTP server that can simultaneously validate or verify email addresses and send multiple email messages. This component is ideal for building email address validator or ...
破解版 TEmailServer is a built-in SMTP server that can simultaneously validate or verify email addresses and send multiple email messages. TEmailServer is an ideal component for building email address...
植物在多种逆境胁迫下的抗逆性解析,巴沙尔,刘继红,The combination of drought, cold and salt factors could occur simultaneously in the field causing a huge loss of crop yields. Hence, the conventional ...
Clone and manage multiple accounts of the same app simultaneously
You can communicate with multiple ports simultaneously. See the Quick Start Guide. MBAXP Technical Facts and Features: RTU Mode. ASCII Mode. Modbus TCP/IP. Multi-threaded for high data throughput. ...
If more than 8 analog input channels are required, multiple cards can be synchronized through the Device-to-Device Bus to support more AI channels simultaneously sampling. The PCI-1706U has two 12-...
simultaneously. This insatiable requirement for multistandard multimode devices has ushered in new innovations in wireless transceiver designs and technologies that led and will continue to lead the...
In order to track multiple particles within a whole cell simultaneously, a parallel tracking approach with large depth of field was put forward. It was based on distorted grating and dual-objective ...
Multiple-input multiple-output (MIMO) communication techniques have been an important area of focus for next-generation wireless systems because of their potential for high capacity, increased ...