views:

723

answers:

2

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 ?

+2  A: 

I assume you are using JPA 2.0 with the incorrect version of Hibernate, which doesn't implement the JPA 2.0 spec. The exception tells that the EntityManagerImpl doesn't have the required method.

I suggest upgrading hibernate to 3.5, which is a JPA 2.0 implementation.

Bozho
Thanks!Just updated my Hibernate JARs to 3.5.0 CR2 and it works great.
Julien
A: 

You can detach all entities with clear but detaching just one entity is not in the JPA 2.0. http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html

You prolly had hibernate impl in your build path, and another implementation on your application server (EclipseLink? or old hibernate version)...

The em.detach is in Hibernate but not in JPA so you need the hibernate impl on your application server to use this function...

Sebastien Lorber
The answer from Bozho resolved my problem.Detach a single entity works FINE in JPA 2.0 (J2EE 6)
Julien
My mistake you are right it's in JPA 2.0 interface
Sebastien Lorber