2011年4月18日 星期一

[Configuration] persistence.xml and applicationContext.xml

* How does the server know which database it is supposed to save / update / query the entity objects? How do we configure the underlying object-relational-mapping engine and cache for better performance and trouble shooting? The persistence.xml file gives you complete flexibility to configure the EntityManager.

* persistence.xml file
  • Is a standard configuration file in JPA.
  • Has to be included in the META-INF directory inside the JAR file that contains the entity beans.
  • The <provider> specifies the underlying implementation of the JPA EntityManager.
  • The <jta-data-source> points to the JNDI name of the database this persistence unit maps to.

* Since you might have multiple instances of persistence-unit defined in the same application, you typically need to explicitly tell the @PersistenceContext annotation which unit you want to inject. For instance, @PersistenceContext(name="myapp") injects the EntityManager from the persistence-unit named "myapp".

* JTA:
  • One of the Java Enterprise Edition (Java EE) APIs allowing distributed transactions to be done across multiple XA resources in a Java environment.
  • Provides for:
    • demarcation of transaction boundaries
    • X/Open XA API allowing resources to participate in transactions.

* <tx:annotation-driven>
  • Enabled spring to interpret @Transactional annotation.
  • Creates a BeanPostProcessor to be included in the BeanPostProcessor step in the initialization phase.
    • When that BeanPostProcessor sees @Transactional in your Spring bean code, then it create a DynamicProxy to proxy that bean so that it will run transactionally.

* 一個 LocalContainerEntityManagerFactoryBean 只能管理一個持久化單元, 僅管其内部創建的DefaultPersistenceUnitManager 類可以得取多個持久化單元信息(PersistenceUnitInfo), 但最终只能有一個被返回给 LocalContainerEntityManagerFactoryBean 用於創建 JPA 實現提供者的 EntityManagerFactory

* Entity Manager 藉由 Persistence Context 來控制被管理的 Entity Instances,而每個 Persistence Context 必定由一個 Persistence Unit 來限制該 Context 可以管理的 Entity Classes 種類。

* Entity Manager 可以分為兩類
  • Container-Managed Entity Manager
    • 是由 JEE Container 所管理的 Entity Manager
    • 主要是以 @PersistenceContext 標識在你的程式中,Container 會在執行期間自動注入對應的 instance。
    • 可以再區分兩類:
      • PersistenceContextType.TRANSACTION (Default):
        • 主要是配合 Stateless Session Bean 與 JTA Transaction,在每次 method 被呼叫時都去檢查 JTA transaction 中是不是有 Persistence Context,如果有就續用否則就建一個新的。
        • 當 JTA Transaction commit 時,在 persistence context 中的 entity instances 就會自動 persist 至 DB。
      • PersistenceContextType.EXTENDED:
        • 主要是配合 Stateful Session Bean,只有當執行到有 @Remove 標識的 method 時才會 persist 在 persistence context 中的 entity instances。
  • Application-Managed Entity Manager
    • Application 自行管理,藉由呼叫 EntityManagerFactory.createEntityManager() 來取得,因此,一般的 J2SE程式也可以使用,當然在 JEE Container 也可以使用,特別是在某些特別情況或有特殊考慮時多一種方式可以應用。
    • 如果要在 JEE Container 中使用的話,與 Container-Managed Entity Manager 不同之處是以 @PersistenceUnit 來標識要被注入的 EntityManagerFactory,而且也必需呼叫 EntityManager.close() 來指出要 persist 的時機。
    • 產生 persistence context 的時機也有所不同,當呼叫 EntityManagerFactory.createEntityManager() 就會產生一個 persistence context。

* JPA allows us to work with entity classes, which are denoted as such using
  • The annotation @Entity.
  • configured in an XML file (we'll call this persistence meta information).
When we acquire the Entity Manager Factory using the Persistence class, the Entity Manager Factory finds and processes the persistence meta information.


* To work with a database using JPA, we need an Entity Manager. Before we can do that, we need to create an Entity Manager Factory.
=> Entity Manager Factory -> Entity Manager -> using JPA...

* To acquire an Entity Manager Factory, we use the class javax.persistence.Persistence.
  • It reads a file called persistence.xml in the META-INF directory.
  • Then creates the named Entity Manager Factory, which processes persistence meta information stored in XML files or annotations (we only use annotations).

* Once we have an Entity Manager, we can ask it to perform several operations such as persisting or removing an entity from the database or creating a query.



* Reference
- 1.2.1. The persistence.xml file
- Java Transaction API
- Understanding working
- 非J2EE 容器环境下Spring +JPA 多持久化单元/多个JAR归档注解实体 的实体扫描问题及解决办法
- JPA2--Transaction Management
- Comments/Suggestions, please email: schuchert -at- yahoo -dot- com

沒有留言:

張貼留言