Java Hibernate 5.2.11-多个同步Featch.Eager
我有一个类,它有4个带有@*toMany注释的列表:Java Hibernate 5.2.11-多个同步Featch.Eager,java,hibernate,jpa,Java,Hibernate,Jpa,我有一个类,它有4个带有@*toMany注释的列表: public class Component { @OneToMany(mappedBy = "component", orphanRemoval = true, cascade = CascadeType.ALL, targetEntity = ComponentHistoric.class) //@OnDelete(action = OnDeleteAction.CASCADE)
public class Component {
@OneToMany(mappedBy = "component", orphanRemoval = true, cascade = CascadeType.ALL, targetEntity = ComponentHistoric.class)
//@OnDelete(action = OnDeleteAction.CASCADE)
@LazyCollection(LazyCollectionOption.FALSE)
private List<ComponentHistoric> componentHistoricList;
@ManyToMany(targetEntity = Organization.class)
@LazyCollection(LazyCollectionOption.FALSE)
private List<Organization> organizations;
@OneToMany(targetEntity = ComponentPerson.class, cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<ComponentPerson> componentPeople;
@OneToMany(targetEntity = ComponentLink.class, cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
private List<ComponentLink> componentLinks;
}
公共类组件{
@OneToMany(mappedBy=“component”,orphanRemoving=true,cascade=CascadeType.ALL,targetEntity=componentHistorical.class)
//@OnDelete(action=OnDeleteAction.CASCADE)
@LazyCollection(LazyCollectionOption.FALSE)
私人名单成分史学家;
@ManyToMany(targetEntity=Organization.class)
@LazyCollection(LazyCollectionOption.FALSE)
私人名单组织;
@OneToMany(targetEntity=ComponentPerson.class,cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
私人名单成员;
@OneToMany(targetEntity=ComponentLink.class,cascade=CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
私有列表组件链接;
}
不能使用集合
而不是列表
。当我使用featch=featch.Eager
时。我得到了一个例外(数字1)。在上面的当前版本中,我得到了异常(编号2)
异常(编号1):org.hibernate.loader.MultipleBagFetchException:无法同时提取多个行李
-
异常(编号2):org.hibernate.LazyInitializationException:延迟初始化角色集合失败:xxxx.xxxxx.xxxx.ComponentHistorical.componentHistoricVersion,无法初始化代理-无会话
例外情况1:
这是Hibernate的一个限制,它在一个实体中不支持多个已加载列表
例外情况2:
当在事务边界之外访问列表时,会出现此异常。因此,必须先初始化此集合,然后才能使用它
这样做有很多选择:
- 调用集合上的getter或迭代器
- 使用Hibernate.initialize()并将集合作为参数传递
- 在使用查询加载实体时,在查询中使用联接提取
- 您没有添加
@LazyCollection(LazyCollectionOption.FALSE)
到xxxx.xxxxx.xxxx.ComponentHistoric.componentHistoricVersion
更好的方法是始终使用延迟加载并加载与Hibernate.initialize()
,加入fetch
等的必要关联,因为您将无法禁用与@LazyCollection
的关联的急切加载