如果您在行使CRUD方法做你必须做这个(交易类型:RESOURCE_LOCAL没有JTA)你每次访问你的数据库时都需要创建EntityManager()吗?
@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?
你需要使用ThreadLocal这种类型的任务。请看这里http://www.naildrivin5.com/daveblog5000/?p=39和这里http://javanotepad.blogspot.com/2007/08/managing-jpa-entitymanager-lifecycle.html - 这是给更多实现细节 – 2012-07-31 14:14:39