Persistence Context
来源:互联网 发布:java 小球跟着鼠标走 编辑:程序博客网 时间:2024/05/10 09:02
Persistence context是由一组受托管的实体对象实例所构成的集合。它受entity manager 的管理。Entity manager追踪persistence context中所有对象的修改和更新情况,并根据指定的flush模式(本章稍后会做讨论)将这些修改保存到数据库中。一旦persistence context被关闭,所有实体对象实例都会脱离EntityManager而成为非托管对象。对象一旦从persistence context中脱离,就不再受entity manager管理了,任何对此对象的状态变更也将不会被同步到数据库。
Java Persistence中有两种类型的persistence context,分别是transaction-scoped persistence context和extended persistence context。
Transaction-scoped persistence context
的persistence context可能只在事务范围内存在,它们会在事务结束后被关闭。
当事务结束时,transaction-scoped persistence context将被销毁,而所有的托管实体对象实例也将处于游离状态(detached)。只有受应用服务器管理的persistence context才可以是事务范围的。换言之,只有标注了@PersistenceContext注解(或是其XML的等价描述)的EntityManager实例才可以是事务范围的。
EJB 3.0中,由于持久对象在脱离persistence context之后将自动变成值对象,因此也就没必要再使用该模式了。
package javax.persistence;
public enum PersistenceContextType
{
TRANSACTION,
EXTENDED
}
public @interface PersistenceProperty {
String name();
String value();
}
@Target({METHOD, TYPE, FIELD}) @Retention(RUNTIME)
public @interface PersistenceContext {
String name() default "";
String unitName() default "";
PersistenceContextType type() default TRANSACTION;
PersistenceProperty[] properties() default {};
}
@PersistenceContext与@PersistenceUnit的作用相仿,唯一的不同在于,注入的是EntityManager实例而非EntityManagerFactory。
类型为EXTENDED的EntityManager只能注入到stateful session bean中。由于stateless session bean和message-driven bean是被缓存的,因而无法关闭persistence context,也无法释放任何受托管的实体实例。一个stateful session bean可以使用type属性为EXTENDED的@javax.persistence.PersistenceContext注解来获取extended context。
如果在EJB中应用Java Persistence,我们强烈建议你使用@Persi- stenceContext注解或等价的XML描述。因为这些功能的目的便是让开发人员更加方便的使用entity bean。相比之下,使用EntityManagerFactory创建EntityManager则因为需要关注更多琐碎的细节而更容易出错。例如,开发人员可能会因为忘记调用close()方法关闭entity manager而导致资源泄漏。请充分利用EJB容器提供给你的便捷设施!
- Persistence Context
- 高校平台--环境理解之Persistence Context
- persistence
- Persistence
- 日志报错:java.lang.IlleagalArgumentExcption: entity not in the persistence context
- Context
- Context
- Context
- context
- Context
- context
- Context
- Context
- Context
- Context
- Context
- Context
- Context
- XML User Interface Language
- 开博
- 现代词故事一则
- 进入SP2.0时代
- 可以方便的将SQL语句的执行结果显示成表格结果的JAVA类,可以用于不同的数据显示
- Persistence Context
- 图文并茂——使用xfire编写webservice,并通过C#调用
- uml的一些网址
- 站在3G大门口
- Invalidate()与UpdateWindow( )
- 美国众神第二十章
- 外企工作7年 痛并快乐着
- 如何提取网页中所有链接
- J2ee通用系统权限设计