在JPA / Hibernate的PostLoad中读取一个集合 [英] Reading a Set in PostLoad on JPA/Hibernate

查看:208
本文介绍了在JPA / Hibernate的PostLoad中读取一个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OneToMany关系的简单实体:

  @Column(name =TITLE)
private字符串_title;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name =PROJECT_ID)
private Set< Device> _设备;
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name =PROJECT_ID)
private Set< Firm> _firms;

我想在PostLoad方法中使用我的设置进行操作。

  @PostLoad 
private void copyToProperty(){
title.set(_title);
if(_devices!= null&&!_devices.isEmpty())
// DO Stuff HERE
}

但是,当我尝试访问_devices时,出现以下异常:

 线程main中的异常javax.persistence.PersistenceException:org.hibernate.LazyInitializationException:无法懒惰地初始化集合,没有会话或会话已关闭
在org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl。
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:635)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:589)
(com.dooapp.jpa.Main.main(Main.java:57))
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java: 39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang .reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

有没有人知道为什么当我在FetchType.EAGER上时我得到一个LazyInitializationException?

不幸的是,Hibernate根本不允许你这样做。在 @PostLoad 方法中,您不允许触摸实体中的任何关联。这是一个长期存在的主要错误。



有关原始错误,请参阅: https: //issues.jboss.org/browse/JBAS-5474 。错误关闭后(仍未解决),立即重新打开一个新的: https: //hibernate.onjira.com/browse/HHH-6043



如果这个bug出错了(我猜是这样),请投票支持。 / p>

I have a simple entity with OneToMany relation :

@Column(name = "TITLE")
private String _title;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "PROJECT_ID")
private Set<Device> _devices;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "PROJECT_ID")
private Set<Firm> _firms;

And I want to do stuff with my set in the PostLoad method

@PostLoad
private void copyToProperty() {
    title.set(_title);
    if (_devices != null && !_devices.isEmpty())
       //DO Stuff HERE
}

But, when I try to access to _devices I get this exception :

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:635)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:589)
at com.dooapp.jpa.Main.main(Main.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Does anyone know why I'm getting a LazyInitializationException as long as I'm on FetchType.EAGER?

And how can do this stuff working?

Thank's

解决方案

Hibernate unfortunately simply doesn't allow you to do that. In an @PostLoad method, you are not allowed to touch any association in the entity. This is a long standing major bug.

For the original bug see: https://issues.jboss.org/browse/JBAS-5474. After the bug was closed (while still unresolved), a new one was immediately re-opened here: https://hibernate.onjira.com/browse/HHH-6043

If this bug bugs you (I guess it does), please vote for it.

这篇关于在JPA / Hibernate的PostLoad中读取一个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆