¿Necesita crear EntityManager () cada vez que accede a su base de datos?

If you are exercising CRUD procedures do you have to do this (with transaction type: RESOURCE_LOCAL not JTA)

@PersistenceUnit(unitName="mongo")
EntityManagerFactory emf;

EntityManager em;


@Inject
private SomeObj injectableObj;

public void create()
{
   em = emf.createEntityManager(); <---- here
   SomeObj obj = new SomeObj();
   em.persist(obj);
}

public void read()
{
   em = emf.createEntityManager();  <---- here
   Query query = em.createQuery("Select s from SomeObj s");

}

public void update()
{
   em = emf.createEntityManager();  <---- here
   SomeObj s = em.find(SomeObj.class, injectableObj.getId());
   s.setSomeObj(injectableObj.getSomeObj());

}

public void delete()
{

   em = emf.createEntityManager();  <---- here
   SomeObj s = em.find(SomeObj.class, injectableObj.getId());
   em.remove(s);
}

Question: Is there anyway to inject the EntityManager?

preguntado el 31 de julio de 12 a las 15:07

you need to use ThreadLocal for such kind of tasks. Please take a look here naildrivin5.com/daveblog5000/?p=39 y aquí javanotepad.blogspot.com/2007/08/… - that's give more implementation details -

2 Respuestas

Maybe try to look here for exemples :

Injections EntityManager

I prefer to use : Injection via @PersistenceContext

Respondido 31 Jul 12, 15:07

This is a NoSQL database. transaction type is RESOURCE_LOCAL NOT JTA. This being said, you cannot use PersistencContext and inject the entitymanager. - stackoverflow

You can use injection. I use it like this:

@PersistenceContext(unitName = "some_jndi_name")
private EntityManager em;

Respondido 31 Jul 12, 15:07

This is a NoSQL database (as denoted by unit_name="mongo"). transaction type is RESOURCE_LOCAL NOT JTA. This being said, you cannot use PersistencContext and inject the entitymanager - stackoverflow

That's fine, do you have any other suggestions? - stackoverflow

Unfortunately, I don't have any experience in NoSQL databases - gkuzmin

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.