JPA的persistence.xml文件


  persistence.xml文件必須定義在classpath路徑下的META-INF文件夾中。

  我們看看基於Hibernate提供的一個比較完整的JPA2.0的persistence.xml文件。

  persistence.xml: 

 1 <?xml version="1.0" encoding="UTF-8"?>
 2  <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
 3              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 4              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 5              http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
 6     
 7     <!--必須要有name屬性,不能為空 -->
 8      <persistence-unit name="jpaPU" transaction-type="RESOURCE_LOCAL">
 9           <!--可選 -->
10           <provider>org.hibernate.ejb.HibernatePersistence</provider>
11           <!--可選 -->
12           <jta-data-source>java:/DefaultDS</jta-data-source>
13           <!--可選 -->
14           <mapping-file>ormap.xml</mapping-file>
15           <!--可選 -->
16           <jar-file>MyApp.jar</jar-file>
17           <!--可選 -->
18           <class>org.acme.Employee</class>
19           <!--可選 -->
20           <shared-cache-mode>ENABLE_SELECTOVE</shared-cache-mode>
21           <!--可選 -->
22           <validation-mode>CALLBACK</validation-mode>
23           
24           <!--廠商的特定屬性 -->
25           <properties>
26              <!--配置Hibernate方言 -->
27              <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
28              <!--配置數據庫驅動 -->
29              <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
30              <!--配置數據庫用戶名 -->
31              <property name="hibernate.connection.username" value="root" />
32              <!--配置數據庫密碼 -->
33              <property name="hibernate.connection.password" value="root" />
34              <!--配置數據庫url -->
35              <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpa?useUnicode=true&amp;characterEncoding=UTF-8" />
36              <!--設置外連接抓取樹的最大深度 -->
37              <property name="hibernate.max_fetch_depth" value="3" />
38              <!--自動輸出schema創建DDL語句 -->
39              <property name="hibernate.hbm2ddl.auto" value="update" />    
40           </properties>
41      </persistence-unit>
42              
43  </persistence>

 

  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"

  要注意使用的是2.0規范

  name  

  JPA2.0規范要求每一個持久化單元必須有一個名字,不能為空。即persistence-unit name="manager1"的name不能為空。

  transaction-type

  使用的事務類型。有JTA和RESOURCE_LOCAL兩種類型可以選擇。在JavaEE環境中默認為JTA,在JavaSE環境中默認為RESOURCE_LOCAL。當在persistent.xml文件使用<jta-data-source>,默認就是JTA事務,使用<non-jta-data-source>,默認就是使用RESOURCE_LOCAL事務。這兩種事務的區別不在這里討論。                

  provider

  EJB Persistence provider的一個實現類。如果不是使用多個廠商的 EJB Persistence實現,是不需要定義的。

  mapping-file

  指定映射文件的位置

  jar-file

  指定要解析的jar。jar中所有注解的類、包和所有的hbm.xml都會被添加到persistent-unit的配置中。主要用在JavaEE環境中。

  exclude-unlisted-classes

  不檢查jar中加了@Entity注解的類。

  class

  明確指定要映射的類

  shared-cache-mode

  緩存模式。加了@Cacheable注解的默認為二級緩存。有四種模式:ALL-緩存所有實體;NONE-禁止緩存;ENABLE_SELECTIVE-如果加了緩存的標識,是默認的選選        項;DISABLE_SELECTIVE- enable caching unless explicitly marked as  @Cacheable(false) (not  recommended)

  validation-mode

  實體的驗證模式,默認是激活的。當一個實體在創建、更新,在實體發送到數據庫前會被進行驗證。CALLBACK: entities are validated on creation, update and deletion. If no Bean Validation provider  is present, an exception is raised at initialization time.

  properties

  配置廠商的一些特定屬性。

  

    

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM