springhibernatewildflyjtatransactional

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


If I run my code with wildfly 14 server I get following excpetion. The same code worked with wildfly 13. Where could be the problem?

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

Solution

  • I have to explicitly load the lazy proxy reference for the merge operation in Wildfly 14. Like this it works.

    Wildfly 13 and before:

    @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);
            }
        }