如果我用wildfly 14服务器运行代码,我将得到以下提示。相同的代码可用于wildfly13。哪里可能出问题了?
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy [ch.example.tool.entity.Benutzer#12] - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:172)
at org.hibernate.proxy.AbstractLazyInitializer.getIdentifier(AbstractLazyInitializer.java:89)
at org.hibernate.internal.SessionImpl.getProxyIdentifier(SessionImpl.java:1752)
at org.hibernate.internal.SessionImpl.getContextEntityIdentifier(SessionImpl.java:1743)
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:271)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:495)
at org.hibernate.type.EntityType.nullSafeSet(EntityType.java:280)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:2868)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3307)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:3229)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3630)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:146)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:604)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:478)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:356)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1454)
... 122 more
答案 0 :(得分:0)
我必须为Wildfly 14中的合并操作显式加载惰性代理引用。这样就可以了。
Wildfly 13及之前版本:
@Transactional
public class VergleichDAOImpl implements VergleichDAO {
@PersistenceContext
private EntityManager em;
@Override
public void save(Vergleich vergleich) {
if(vergleich.getId() == null) {
em.persist(vergleich);
} else {
em.merge(vergleich);
}
}
Wildfly 14
@Transactional
public class VergleichDAOImpl implements VergleichDAO {
@PersistenceContext
private EntityManager em;
@Override
public void save(Vergleich vergleich) {
if(vergleich.getId() == null) {
em.persist(vergleich);
} else {
Benutzer benutzer = em.find(Benutzer.class, vergleich.getBenutzer().getId());
vergleich.setBenutzer(benutzer);
em.merge(vergleich);
}
}
答案 1 :(得分:0)
我还选择了一种解决方法,所有实体@ManyToOne都已使用@NotAudited进行注释,因此Envers不会在entityManager关闭后尝试加载它们。目前有效,似乎是一个Hibernate错误。
答案 2 :(得分:0)
在this article中,可以在配置中设置hibernate.jpa.compliance.proxy=false
(因为Wildfly可能将其设置为true)。