Detach an entity from a JPA persistence context (JPA 2.0 / Hibernate / EJB 3 / J2EE 6)

Posted by Julien on Stack Overflow See other posts from Stack Overflow or by Julien
Published on 2010-03-29T14:50:14Z Indexed on 2010/03/29 15:03 UTC
Read the original article Hit count: 1065

Filed under:
|
|
|

Hi,

I wrote a stateless EJB method allowing to get an entity in "read-only" mode.

The way to do this is to get the entity with the EntityManager then detach it (using the JPA 2.0 EntityManager).

My code is the following:

@PersistenceContext
private EntityManager entityManager;

public T getEntity(int entityId, Class<T> specificClass, boolean readOnly) throws Exception{
  try{
    T entity = (T)entityManager.find(specificClass, entityId);
    if (readOnly){
      entityManager.detach(entity);
    }
    return entity;
  }catch (Exception e){
    logger.error("", e);
    throw e; 
  }
}  

Getting the entity works fine, but the call to the detach method returns the following error:

GRAVE: javax.ejb.EJBException
    at ...
Caused by: java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerImpl.detach(Ljava/lang/Object;)V
    at com.sun.enterprise.container.common.impl.EntityManagerWrapper.detach(EntityManagerWrapper.java:973)
    at com.mycomp.dal.MyEJB.getEntity(MyEJB.java:37)

I can't get more information and don't understand what the problem is...

Could somebody help ?

© Stack Overflow or respective owner

Related posts about jpa

Related posts about hibernate