开发者

Reading a Set in PostLoad on JPA/Hibernate

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 clo开发者_如何学Csed
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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜