JPA的persistence.xml檔案

來源:互聯網
上載者:User

  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

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

  

    

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.