【文档摘要】J2EE Persistence - Introduction to the Java Persistence API【Managing Entities】
2014-11-19 22:15
549 查看
Entity Manager
每个Entity Manager都有一个对应的Persistent Context相关,Persistent Context定义了一个scope,所有关于Entity的更新、删除等操作都是在这个scope中进行的额。而我们又是通过Entity Manager和Persistent Context进行交互的。
1. Entity Manager Interface
1)Container-Managed Entity Managers
当Application的Container(如JEE Container或如Spring这样custom container)管理了Entity Manager的lifecycle,则称这个Entity Manager是Container-Managed
在这种情况下,EntityManager实例是通过@PersistentContext注入的。
a) Transaction Scoped Entity Manager
这个是最常见Entity Manager的使用方式,这样最大的好处是Transaction Scoped Entity Manager是无状态的(Stateless)。这样同时使Entity Manager使用起来是线程安全的,并且是透明的。为什么Transaction Scoped Entity Manager是Stateless的呢?这是因为Container Managed Entity Managers依赖于JTA Transaction。当我们调用Container
Managed Entity Managers时,Container会为entityManager创建一个proxy,这个proxy就会负责check当前的entityManager对应的Persistent Context是否有对应的有JTA Transaction。如果有,就使用当前的transaction;如果没有,则创建新的。
对于只读的情况,我们可以标记@TransactionAttribute为NOT_SUPPORTED,将不会使用transaction,从而提高效率。
b) Extended Scope Entity Manager
需要@Stateful
2)Application-Managed Entity Manager
在这种情况下,Entity Manager的Lifecycle是通过Application自己管理的。并且需要考虑线程安全等问题。(略)
3. Finding Entities Using Entity Manger
4. Managing Entity Manager's Lifecycle
5. Persisting Entity Instances
6. Removing Entity Instances
7. Synchronizing Entity Data to Database
当entity manager对应的事务提交时,Entity的数据将会被更新到数据库。当需要强制更新到数据库时,可以调用entityManager的flush方法。
2. Persistent Units
定义了在application中被Entity Manager中管理的一系列类:
如上配置,都放在persistence.xml,放在META-INF下。
参考资料:
https://docs.oracle.com/javaee/7/tutorial/doc/persistence-intro003.htm
http://www.kumaranuj.com/2013/06/jpa-2-entitymanagers-transactions-and.html
每个Entity Manager都有一个对应的Persistent Context相关,Persistent Context定义了一个scope,所有关于Entity的更新、删除等操作都是在这个scope中进行的额。而我们又是通过Entity Manager和Persistent Context进行交互的。
1. Entity Manager Interface
1)Container-Managed Entity Managers
当Application的Container(如JEE Container或如Spring这样custom container)管理了Entity Manager的lifecycle,则称这个Entity Manager是Container-Managed
在这种情况下,EntityManager实例是通过@PersistentContext注入的。
@PersistenceContext EntityManager entityManager;
a) Transaction Scoped Entity Manager
这个是最常见Entity Manager的使用方式,这样最大的好处是Transaction Scoped Entity Manager是无状态的(Stateless)。这样同时使Entity Manager使用起来是线程安全的,并且是透明的。为什么Transaction Scoped Entity Manager是Stateless的呢?这是因为Container Managed Entity Managers依赖于JTA Transaction。当我们调用Container
Managed Entity Managers时,Container会为entityManager创建一个proxy,这个proxy就会负责check当前的entityManager对应的Persistent Context是否有对应的有JTA Transaction。如果有,就使用当前的transaction;如果没有,则创建新的。
对于只读的情况,我们可以标记@TransactionAttribute为NOT_SUPPORTED,将不会使用transaction,从而提高效率。
b) Extended Scope Entity Manager
需要@Stateful
2)Application-Managed Entity Manager
在这种情况下,Entity Manager的Lifecycle是通过Application自己管理的。并且需要考虑线程安全等问题。(略)
3. Finding Entities Using Entity Manger
4. Managing Entity Manager's Lifecycle
5. Persisting Entity Instances
6. Removing Entity Instances
7. Synchronizing Entity Data to Database
当entity manager对应的事务提交时,Entity的数据将会被更新到数据库。当需要强制更新到数据库时,可以调用entityManager的flush方法。
2. Persistent Units
定义了在application中被Entity Manager中管理的一系列类:
<persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers. It does not rely on any vendor-specific features and can therefore be deployed to any persistence provider. </description> <jta-data-source>jdbc/MyOrderDB</jta-data-source> <jar-file>MyOrderApp.jar</jar-file> <class>com.widgets.CustomerOrder</class> <class>com.widgets.Customer</class> </persistence-unit> </persistence>
如上配置,都放在persistence.xml,放在META-INF下。
参考资料:
https://docs.oracle.com/javaee/7/tutorial/doc/persistence-intro003.htm
http://www.kumaranuj.com/2013/06/jpa-2-entitymanagers-transactions-and.html
相关文章推荐
- 【文档摘要】J2EE Persistence - Introduction to the Java Persistence API【Querying Entities】
- 【文档摘要】J2EE Persistence - Introduction to the Java Persistence API【Entities Inheritance】
- 【文档摘要】J2EE Persistence - Introduction to the Java Persistence API【Entities】
- Jena文档《An Introduction to RDF and the Jena RDF API》的译文
- An Introduction to the Java Logging API (part)
- 【文档摘要】J2EE Persistence - Controlling Concurrent Access to Entity Data with Locking
- Introduction to using Java Persistence API in a web application in Java EE environment
- The Basics of the MMAPI for Java Developers -> Introduction to the MMAPI
- Jena文档《An Introduction to RDF and the Jena RDF API》的译文
- Introduction to the Solaris Doors API
- Upgrading to Java 8——第四章 The Stream API
- Website for the introduction to Matlab and Java
- An Introduction to RDF and the Jena RDF API
- Using the Java Persistence API with Amazon SimpleDB
- Website for the introduction to Matlab and Java
- Website for the introduction to Matlab and Java
- HOW-TO: Handling cookies using the java.net.* API
- Combining Groovy, Grails, MySQL, and the Java Persistence API
- How-To : Using EJB 3.0 and Java Persistence API with Spring in OC4J
- An Introduction to the JAIN SIP API