Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 779
Julien Avatar asked Mar 29 '10 14:03

Julien


People also ask

How do you remove entity from persistence context?

You can prevent that by calling the flush method on the EntityManager. After you've done that, you can remove a specific entity from the persistence context by calling the detach method or you can call the clear method to clear the persistence context completely.

What does EntityManager detach do?

detach. Remove the given entity from the persistence context, causing a managed entity to become detached. Unflushed changes made to the entity if any (including removal of the entity), will not be synchronized to the database. Entities which previously referenced the detached entity will continue to reference it.

How is an EntityManager configured JPA?

Entity managers are derived from factory interface called EntityManagerFactory. The configuration used by the entity manager is actually created by the entity manager factory. This definition is separately called a persistent unit. The persistent unit lays out both implicit and explicit settings, entity classes.

What is hibernate EntityManagerFactory?

EntityManager. The EntityManager API is used to access a database in a particular unit of work. It is used to create and remove persistent entity instances, to find entities by their primary key identity, and to query over all entities. This interface is similar to the Session in Hibernate. Persistence context.

What is JPA detached entity in JPA?

JPA - Detaching an Entity Instance from the Persistence Context. An entity becomes detached (unmanaged) on following actions: serializing or sending an entity remotely (pass by value). Changes to a detached entity (changing fields, removing, refreshing etc) will not be synchronized to the database state.

How to clear the persistence context of a detached entity?

by clearing the persistence context with EntityManager.clear () serializing or sending an entity remotely (pass by value). Changes to a detached entity (changing fields, removing, refreshing etc) will not be synchronized to the database state. A detached entity still has the database identity.

How does EntityManager work with persistence?

The EntityManager will create a temporary persistence context, perform the find operation, end the persistence context, and return the detached result object to you. A second call with the same id will return a second detached object.

What is persistence context in hibernate?

Overview Persistence providers like Hibernate make use of persistence context to manage the entity lifecycle in an application. In this tutorial, we'll start with the introduction of the persistence context, then we'll see why it's important.


2 Answers

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.

like image 108
Bozho Avatar answered Oct 22 '22 15:10

Bozho


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 probably had hibernate impl in your build path, and another implementation on your application server (EclipseLink? or old hibernate version)...

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

like image 35
Sebastien Lorber Avatar answered Oct 22 '22 16:10

Sebastien Lorber