1

如果您正在执行 CRUD 程序,您是否必须这样做(事务类型:RESOURCE_LOCAL 而不是 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);
}

问题:有没有注入EntityManager?

4

2 回答 2

1

Maybe try to look here for exemples :

Injections EntityManager

I prefer to use : Injection via @PersistenceContext

于 2012-07-31T14:12:19.437 回答
0

You can use injection. I use it like this:

@PersistenceContext(unitName = "some_jndi_name")
private EntityManager em;
于 2012-07-31T14:13:38.907 回答