EJB持久层cannot simultaneously fetch multiple bags异常的解决
病理特征:Caused by: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags,
堆栈信息:org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'initSystemDataBySpringListeren': Injection of
resource fields failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'service': Injection of resource fields failed; nested
exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'assistantDao': Injection of resource
fields failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating
bean with name 'sessionFactory' defined in class path resource
[spring-core-config.xml]: Invocation of init method failed; nested
exception is org.hibernate.HibernateException: cannot simultaneously
fetch multiple bags
......
cannot simultaneously fetch multiple
bags异常是由于持久层实时加载太多异同对象而致。例如用户登录时,同步实时加载用户的角色对象、权限对象,而往往这些关系都是多对多关系,就单一列内
容来看存在重复的值,从而引起multiple
bags。我在网上搜索这类结果,几乎都提出一个解决办法——采用延迟加载,即fetch=FetchType.LAZY,这样就束缚了框架的强大功能,
也有提过用@IndexColumn解决,但@IndexColumn是Hibernate的东西,不是JPA规范(下文有讨论)。既要实时加载,又保证
不会出现主题问题,这就是本文所要探讨的。
JPA规范中,一对多或多对多的多方数据抓取过来后必须用容器类存,例如Set、List、Map等,初学者可能没有对这个几个容器认真研究,随便拿一个
就用。事实上这几个容器有很大的区别,本文不具体讨论这些容器的区别和功能,但提一个特别要注意的区别——导致本文主题的产生——就是容器内是否允许重复
值,那让们简单地了解下这此容器的特性:
Set:
_Set中不允许存放重复元素;
_Set中的元素是无序的。
List:
_List中可以存放重复元素;
_List中的元素是一个有序的集合,可以通过访问List中的元素。
Map:
_Map是以键/值存放数据,因此它有较高的存取性能;
_Map中不允许重复的键,但可以有重复的值
有了这些区别,我们就可以开始寻找主题问题,当持久框架抓取一方的对象时,同时加载多方的对象放进容器中,多方又可能与关联其它对象,Hibernate
实现的JPA,默认最高抓取深度含本身级为四级(它有个属性配置是0-3),若多方(第二级)存在重复值,则第三级中抓取的值就无法映射,按照这个道理,
就应该报出无法同时加载多个包之异常。由于国内EJB3.0以后的教材是少而甚少,更没有较完善的JPA手册,所以笔者这样的理解可能会引起质疑,但事实
上,笔者是通过这个思路解决了问题:
即@ManyToMany或@OneToMany的Many方此时一定用Set容器来存放,而不能用List集合。
不过Hibernate有些功能超越了JPA规范,它支持真正的List集合,映射集合的方式和以前完全一样,只不过要新增 @IndexColumn注解,
该注解允许你指明存放索引值的字段。但实际上是创建唯一性索引,抓取多方的结果也是唯一的,就是上述Set容器不允许重复元素的道理一样。
出现此异常的读者,先看看自己是不是用了List集合而导致此问题的发生,若是用Set还出现此问题,则去看Set容器内的对象的类中是不是还有类似问题。
作者:张纪豪(转载请注明出外)
分享到:
相关推荐
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
"Modeling and Simultaneously Removing Bias via Adversarial Neural Networks"这篇论文提出了一种新颖的方法,通过对抗性神经网络(Adversarial Neural Network, ANN)来同时建模并消除这种偏差。 传统的解决偏差...
为解决这一问题,文章介绍了一种新开发的工业级体积脉动注射成型(VPIM)机,该机能够在整个注射成型过程中引入高强度振动场。作为一种一步成型的微观结构控制手段,振动场能够驱动分散的PET微滴形成刚性的微纤维,...
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 ...
聚合物加工中的自强化和自增韧是指在不损害材料流动性的情况下,通过物理或化学方法提高聚合物的力学性能,如冲击强度和屈服强度等。高密度聚乙烯(HDPE)作为一种常见的工程塑料,其优异的化学稳定性、良好的加工性...
文章标题和描述中提到的主题是“具有负电常数ε和磁导率μ的同时值的物质的电动力学”。这一领域涉及电磁波在物质中的传播特性,并且特别关注了介质对电磁波响应的两个基本物理量:电常数ε和磁导率μ。...
这篇论文探讨的是一对多充电模式——即一个移动充电器同时为多个传感器充电,从而有效解决了传感器网络中充电的可扩展性和效率问题。 首先,论文提出了一种新型的充电技术,即一对多充电方案,这是一种允许一个充电...
在研究中,使用了共轭梯度法(Conjugate Gradient Method, CGM)和商业计算流体动力学软件CFD-ACE+来解决此问题。 逆几何问题在传热领域有着重要的应用,尤其是当遇到直接热传导问题无法解决时。在直接热传导问题中...
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 ...
汽车主动前轮转向与防抱死制动系统集成控制研究-汽车主动前轮转向与防抱死制动系统集成控制研究.rar ...system can keep the vehicle stable and shorten the braking distance simultaneously.
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...
超市闭路电视监控系统解决方案 超市闭路电视监控系统解决方案是一种综合性解决方案,旨在提供一个高效、可靠、实时的监控系统,以满足超市的安全需求。该系统由闭路监控系统、防盗报警系统和主控室配置三部分组成。...
or installed on a single workstation used non-simultaneously by multiple people, but not both. You may access the registered version of SVGDeveloper through a network, provided that you have obtained...
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-...
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 ...