cannot simultaneously fetch multiple bags

问题是什么时候出现的呢?
    当一个实体对象中包含多于一个non-lazy获取策略时,比如@OneToMany,@ManyToMany或者@ElementCollection时,获取策略为(fetch = FetchType.EAGER)

出现问题的原因:
    当(fetch = FetchType.EAGER)多余一个时,持久框架抓取一方的对象时,同时又将多方的对象加载进容器中,多方又可能关联其它对象,Hibernate实现的JPA,默认最高抓取深度含本身级为四级(它有个属性配置是0-3),若多方(第二级)存在重复值,则第三级中抓取的值就无法映射,就会出现 multiple bags。

解决方法:
    1、将(fetch = FetchType.EAGER)改为(fetch = FetchType.LAZY)
    2、将List修改成Set集合,即推荐@ManyToMany或@OneToMany的Many方此时用Set容器来存放,而不用List集合。
    3、改变FetchMode为@Fetch(FetchMode.SUBSELECT),即发送另外一条select语句抓取前面查询到的所有实体对象的关联实体。
    4、在对应的属性上添加@IndexColumn,该注解允许你指明存放索引值的字段,目的跟Set容器不允许重复元素的道理一样。

推荐的处理办法:方法2;
    方法3和方法4是Hibernate特有的,非JPA标准;
    如果可以用方法1,那就不会出现这个问题。

时间: 2024-10-08 17:52:14

cannot simultaneously fetch multiple bags的相关文章

[转]cannot simultaneously fetch multiple bags 问题的解决办法

完整的异常信息如下: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94) at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119) a

“cannot simultaneously fetch multiple bags”的解决方法

Java代码   @OneToMany(mappedBy = "customer",cascade = {CascadeType.ALL},fetch = FetchType.LAZY) private Collection<CartItem> cartItems; @OneToMany(mappedBy = "customer",cascade = {CascadeType.ALL},fetch = FetchType.EAGER) private C

ejb3persistence.jar javax.persistence的注解配置

JPA注解持久化类很方便,需要jar包:ejb3-persistence.jar.我用以下三个类来说明用法.  sh原创 转载请注明: http://67566894.iteye.com/blog/659829 Java代码    @SuppressWarnings ( "serial" ) @Entity @Table (name= "T_X" ) public   class  X  implements  Serializable { @Id @Generat

JPA注解指南

PA注解持久化类很方便,需要jar包:ejb3-persistence.jar.我用以下三个类来说明用法. 1 @SuppressWarnings("serial") 2 @Entity 3 @Table(name="T_X") 4 public class X implements Serializable 5 { 6 @Id 7 @GeneratedValue(strategy = GenerationType.AUTO) 8 private int id; 9

Git CMD - fetch: Download objects and refs from another repository

命令格式 git fetch [<options>] [<repository> [<refspec>…?]] git fetch [<options>] <group> git fetch --multiple [<options>] [(<repository> | <group>)…?] git fetch --all [<options>] 命令参数 --dry-run 不执行任何操作,只显

git fetch 命令

git fetch命令用于从另一个存储库下载对象和引用. 使用语法 git fetch [<options>] [<repository> [<refspec>-]] git fetch [<options>] <group> git fetch --multiple [<options>] [(<repository> | <group>)-] git fetch --all [<options>

Oracle Applications Multiple Organizations Access Control for Custom Code

文档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code Checked for relevance on 12-JAN-2011 See Change Record This document discusses how to update the customization code that is affected by the access co

General-Purpose Operating System Protection Profile

1 Protection Profile Introduction ? This document defines the security functionality expected to be provided by a general-purpose operating system capable of operating in a networked environment. It also provides a set of assurance components that de

PHP MysqlI操作数据库(转)

1连接数据库. Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->//procedural style $mysqli =  mysqli_connect('host','username','password','database_name');//object oriented style (recommended) $mysqli = new