容器托管EntityManager
@PersistenceContext,存放unitName指向的DataBase对应的EntityBean实例集合,以及对这些实例进行生命周期管理
@PersistenceContext(
name="entityManagerName",
properties=@PersistenceProperty[],
type=""//PersistenceContextType TRANSACTION (default,transaction- scope) / EXTENDED
unitName=""//the persistence unit name
)
EntityManager
必须关联一个PersistenceContext对象,提供一系列方法对PersistenceContext中的EntityBean实例进行操作。
可以使用依赖注入,将某个PersistenceContext对象注入给EntityManager对象
如:
@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager;
JAVAEE容器会智能选取容器中的PersistenceContext实例跟这个entityManager实例来关联
a、一般情况下,一个persistence unit只需对应一个PersistenceContext实例,如
class UserDao{
@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager;
}
class OrderDao{
@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager;
}
UserDao和OrderDao中的entityManager对象关联的PersistenceContext是同一个实例
当然,我们也可以手动为PersistenceUnit增加PersistenceContext实例
class UserDao{@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager;
@PersistenceContext(name="pc2" ,unitName="lxhPU")
private EntityManager entityManager2;
}
UserDao 中的entityManager和entityManager2关联的2个PersistenceContext实例是不同的,尽管他们指向的是同一个PersistenceUnit
b、JAVAEE容器会维护PersistenceContext实例及其关联的EntityManger实例
一个PersistenceContext实例可以被多个EntityManager实例关联
class UserDao{@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager;
@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager2;
}
class OrderDao{
@PersistenceContext(unitName="lxhPU")
private EntityManager entityManager;
}
还是上面的代码:
UserDao和OrderDao中的entityManager对象关联的PersistenceContext是同一个实例,其实这2个entityManager也是指向容器中的同一个EntityManager实例
但是UserDao中的entityManager和entityManager2却是不同的EntityManager实例,在同一个类中多次定义,JAVAEE容器会为他们分配不同的EntityManager实例,无则新建,而这2个实例所关联的PersistenceContext对象却是同一个。
做个简单的测试就一目了然了
@Stateless@Remote
public class UserDao1 implements IUserDiffEmDao{
@PersistenceContext(unitName="lxhPU")
private EntityManager em;
@PersistenceContext(unitName="lxhPU")
private EntityManager em2;
@PersistenceContext(name="pc3", unitName="lxhPU")
private EntityManager em3;
@PersistenceContext(unitName="lxh2PU")
private EntityManager em4;
public void test() {
System.out.println("-----------------------");
System.out.println("em="+em);//----------------EntityManager@001
System.out.println("em2="+em2);//----------------EntityManager@002
System.out.println("em3="+em3);//----------------EntityManager@003
System.out.println("em4="+em4);//----------------EntityManager@004
<span style="color: #008000;">//4个结果是不一样的</span>
return null;
}
}
@Stateless
@Remote
public class UserDao2 implements IUserDiffEmDao{
@PersistenceContext(unitName="lxhPU")
private EntityManager em;
@PersistenceContext(unitName="lxhPU")
private EntityManager em2;
@PersistenceContext(name="pc3", unitName="lxhPU")
private EntityManager em3;
@PersistenceContext(unitName="lxh2PU")
private EntityManager em4;
public void test() {
System.out.println("-----------------------");
System.out.println("em="+em);//----------------EntityManager@001
System.out.println("em2="+em2);//----------------EntityManager@002
System.out.println("em3="+em3);//----------------EntityManager@003
System.out.println("em4="+em4);//----------------EntityManager@004
<span style="color: #008000;">//4个结果是不一样的,但是其值分别对应UserDao1打印的4个值</span>
try {
PersistenceContext context1 = this.getClass().getDeclaredField("em").getAnnotation(PersistenceContext.class);
//-----PersistenceContext@001
PersistenceContext context2 = this.getClass().getDeclaredField("em2").getAnnotation(PersistenceContext.class);
//-----PersistenceContext@001
PersistenceContext context3 = this.getClass().getDeclaredField("em3").getAnnotation(PersistenceContext.class);
//-----PersistenceContext@002
PersistenceContext context4 = this.getClass().getDeclaredField("em4").getAnnotation(PersistenceContext.class);
//-----PersistenceContext@003
System.out.println("UserDao2.context1="+context1.hashCode());System.out.println("UserDao2.context2="+context2.hashCode());
System.out.println("UserDao2.context3="+context3.hashCode());
System.out.println("UserDao2.context4="+context4.hashCode());
<span style="color: #008000;">//context1和context2指向的是同一个PersistenceContext实例</span>
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchFieldException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}