wildfly 14 的 lazyinitalizationexception:无法初始化代理 - 拥有的会话已关闭

lazyinitalizationexception with wildfly 14: could not initialize proxy - the owning session was closed

如果我 运行 我的代码使用 wildfly 14 服务器,我会遇到以下异常。相同的代码适用于 wildfly 13。问题可能出在哪里?

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

我必须为 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);
        }
    }

野蝇 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);
        }
    }

我还选择了一种解决方法,所有实体@ManyToOne 都已使用@NotAudited 进行注释,因此 Envers 不会在 entityManager 关闭后尝试加载它们。目前有效,似乎是一个 Hibernate 错误。

this article 开始,在您的配置中设置 hibernate.jpa.compliance.proxy=false 可能是解决方案(因为 Wildfly 可能将其设置为 true)。