2016-10-27 12 views
0

EntityManagerインスタンスを挿入するコンテナ管理のJPAを使用します。注入されたentitymanagerインスタンスでは、find()メソッドを使用すると、エンティティマネージャが閉じられています。 wildfly 10を使用しています。Hibernate EntityManagerが閉鎖されています(wildfly 10)

どうすればこの問題を解決できますか?私はここで間違っていますか?

このようなエンティティマネージャを作成します。

 @PersistenceContext 
     protected EntityManager em; 


    @Stateless 
    public class CustomerService CrudService<Customer> { 


    @TransactionAttribute(TransactionAttributeType.REQUIRED) 
    public void update(Customer entity) { 

    Customer item = em.find(Customer.class,entity.getId()); //ISSUE 
    if (entity.getParentId()!=null) { 
     item.setParent(em.find(CRMEntity.class , entity.getParentId())); 
    item.setParentId(entity.getParentId()); 
    } 
.... 

super.update(item); 
} 

public abstract class CrudService<T extends BaseEntity> { 
public void update(T entity) { 
     Session session = null; 

     try { 
      session = getSession(); 
      session.update(entity); 
      session.flush(); 

     } catch (HibernateException ex) { 
      log.error("Error when update." + entity.getCode(), ex); 
      if (session != null) { 
       session.cancelQuery(); 
      } 
     } finally { 
      if (session != null && session.isOpen()) { 
       session.clear(); 
       session.close(); 
      } 
     } 
    } 
public Session getSession() { 
     if (session == null || !session.isOpen()) { 
      session = getEntityManager().unwrap(Session.class); 
     } 
     return session; 
    } 

} 

この行で問題が発生します。

Customer item = em.find(Customer.class,entity.getId()); 

エラー

Caused by: java.lang.IllegalStateException: Session/EntityManager is closed 
    at org.hibernate.internal.AbstractSharedSessionContract.checkOpen(AbstractSharedSessionContract.java:326) 
    at org.hibernate.engine.spi.SharedSessionContractImplementor.checkOpen(SharedSessionContractImplementor.java:126) 
    at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3312) 
    at org.hibernate.internal.SessionImpl.find(SessionImpl.java:3297) 
    at org.jboss.as.jpa.container.AbstractEntityManager.find(AbstractEntityManager.java:213) 
    at com.leightonobrien.lob2.service.autogen.CustomerService.update(CustomerService.java:412) 
    at sun.reflect.GeneratedMethodAccessor249.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:497) 
    at org.jboss.as.ee.component.ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptor.java:52) 
    at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:340) 
    at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:437) 
+0

なぜあなたはスプリングで提供されるトランザクションを使用していません。 –

+0

"@VibhaySachan私たちは職場で春を使用していません。wildflyでHibernateを使用します。 – Ratha

+0

次に、javax.transaction.Transactionalを使用できます。 –

答えて

1

問題は、ここで私がセッションを閉じています。それを取り除いた後、すべて正常に動作します

関連する問題