首页 > 代码库 > jpa persistence.xml文件配置详解

jpa persistence.xml文件配置详解

1.1.  示例

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="jpaDemo" transaction-type="RESOURCE_LOCAL">
      <properties>
         <property name="hibernate.dialect" value=http://www.mamicode.com/"org.hibernate.dialect.MySQL5Dialect"/>>

1.2.  详细说明

Persistence.xml

<?xml version="1.0" encoding="UTF-8"?> 

 

<persistenceversion="1.0" 

xmlns:persistence="http://java.sun.com/xml/ns/persistence" 

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

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

 

<!--   

     Name属性用于定义持久化单元的名字 (name必选,空值也合法);  

     transaction-type 指定事务类型(可选)   

--> 

<persistence-unitname="unitName" transaction-type="JTA"> 

 

   <!-- 描述信息.(可选) --> 

   <description></description> 

 

   <!-- javax.persistence.PersistenceProvider接口的一个实现类(可选) --> 

   <provider>  </provider> 

 

   <!-- Jta-data-source和 non-jta-data-source用于分别指定持久化提供商使用的JTA和/或non-JTA数据源的全局JNDI名称(可选) --> 

   <jta-data-source>java:/MySqlDS</jta-data-source> 

   <non-jta-data-source></non-jta-data-source> 

 

   <!-- 声明orm.xml所在位置.(可选) --> 

   <mapping-file>product.xml</mapping-file> 

 

   <!-- 以包含persistence.xml的jar文件为基准的相对路径,添加额外的jar文件.(可选) --> 

   <jar-file>../lib/model.jar</jar-file> 

 

   <!-- 显式列出实体类,在Java SE 环境中应该显式列出.(可选) --> 

   <class>com.domain.User</class> 

   <class>com.domain.Product</class> 

 

   <!-- 声明是否扫描jar文件中标注了@Enity类加入到上下文.若不扫描,则如下:(可选) --> 

   <exclude-unlisted-classes/> 

 

   <!--   厂商专有属性(可选)   --> 

   <properties> 

    <!-- hibernate.hbm2ddl.auto= create-drop / create / update --> 

    <propertyname="hibernate.hbm2ddl.auto" value="update"/> 

    <propertyname="hibernate.show_sql" value="true"/> 

   </properties> 

 

</persistence-unit> 

</persistence>

 

1.3.  Xsd文件

persistence_1_0.xsd

<?xml version="1.0" encoding="UTF-8"?>

<!-- persistence.xml schema -->

<xsd:schema targetNamespace="http://java.sun.com/xml/ns/persistence"

  xmlns:xsd="http://www.w3.org/2001/XMLSchema"

  xmlns:persistence="http://java.sun.com/xml/ns/persistence"

  elementFormDefault="qualified"

  attributeFormDefault="unqualified"

  version="1.0">

 

  <xsd:annotation>

    <xsd:documentation>

      @(#)persistence_1_0.xsd  1.0  Feb 9 2006

    </xsd:documentation>

  </xsd:annotation>

 

  <xsd:annotation>

    <xsd:documentation>

 

      DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.

 

      Copyright 2005-2007 Sun Microsystems, Inc. All rights reserved.

 

      The contents of this file are subject to the terms of either the

      GNU General Public License Version 2 only ("GPL") or the Common

      Development and Distribution License("CDDL") (collectively, the

      "License").  You may not use this file except in compliance with

      the License. You can obtain a copy of the License at

      https://glassfish.dev.java.net/public/CDDL+GPL.html or

      glassfish/bootstrap/legal/LICENSE.txt.  See the License for the

      specific language governing permissions and limitations under the

      License.

 

      When distributing the software, include this License Header

      Notice in each file and include the License file at

      glassfish/bootstrap/legal/LICENSE.txt.  Sun designates this

      particular file as subject to the "Classpath" exception as

      provided by Sun in the GPL Version 2 section of the License file

      that accompanied this code.  If applicable, add the following

      below the License Header, with the fields enclosed by brackets []

      replaced by your own identifying information:

      "Portions Copyrighted [year] [name of copyright owner]"

 

      Contributor(s):

 

      If you wish your version of this file to be governed by only the

      CDDL or only the GPL Version 2, indicate your decision by adding

      "[Contributor] elects to include this software in this

      distribution under the [CDDL or GPL Version 2] license."  If you

      don‘t indicate a single choice of license, a recipient has the

      option to distribute your version of this file under either the

      CDDL, the GPL Version 2 or to extend the choice of license to its

      licensees as provided above.  However, if you add GPL Version 2

      code and therefore, elected the GPL Version 2 license, then the

      option applies only if the new code is made subject to such

      option by the copyright holder.

 

    </xsd:documentation>

  </xsd:annotation>

 

   <xsd:annotation>

     <xsd:documentation><![CDATA[

 

     This is the XML Schema for the persistence configuration file.

     The file must be named "META-INF/persistence.xml" in the

     persistence archive.

     Persistence configuration files must indicate

     the persistence schema by using the persistence namespace:

 

     http://java.sun.com/xml/ns/persistence

 

     and indicate the version of the schema by

     using the version element as shown below:

 

      <persistence xmlns="http://java.sun.com/xml/ns/persistence"

        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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

          http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"

        version="1.0">

          ...

      </persistence>

 

    ]]></xsd:documentation>

  </xsd:annotation>

 

  <xsd:simpleType name="versionType">

    <xsd:restriction base="xsd:token">

      <xsd:pattern value=http://www.mamicode.com/"[0-9]+(/.[0-9]+)*"/>

    </xsd:restriction>

  </xsd:simpleType>

 

  <!-- **************************************************** -->

 

  <xsd:element name="persistence">

    <xsd:complexType>

      <xsd:sequence>

 

        <!-- **************************************************** -->

 

        <xsd:element name="persistence-unit"

                     minOccurs="0" maxOccurs="unbounded">

          <xsd:complexType>

            <xsd:annotation>

              <xsd:documentation>

 

                Configuration of a persistence unit.

 

              </xsd:documentation>

            </xsd:annotation>

            <xsd:sequence>

 

            <!-- **************************************************** -->

 

              <xsd:element name="description" type="xsd:string"

                           minOccurs="0">

                <xsd:annotation>

                  <xsd:documentation>

 

                    Textual description of this persistence unit.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="provider" type="xsd:string"

                           minOccurs="0">

                <xsd:annotation>

                  <xsd:documentation>

 

                    Provider class that supplies EntityManagers for this

                    persistence unit.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="jta-data-source" type="xsd:string"

                           minOccurs="0">

                <xsd:annotation>

                  <xsd:documentation>

 

                    The container-specific name of the JTA datasource to use.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="non-jta-data-source" type="xsd:string"

                           minOccurs="0">

                <xsd:annotation>

                  <xsd:documentation>

 

                    The container-specific name of a non-JTA datasource to use.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="mapping-file" type="xsd:string"

                           minOccurs="0" maxOccurs="unbounded">

                <xsd:annotation>

                  <xsd:documentation>

 

                    File containing mapping information. Loaded as a resource

                    by the persistence provider.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="jar-file" type="xsd:string"

                           minOccurs="0" maxOccurs="unbounded">

                <xsd:annotation>

                  <xsd:documentation>

 

                    Jar file that should be scanned for entities.

                    Not applicable to Java SE persistence units.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="class" type="xsd:string"

                           minOccurs="0" maxOccurs="unbounded">

                <xsd:annotation>

                  <xsd:documentation>

 

                    Class to scan for annotations.  It should be annotated

                    with either @Entity, @Embeddable or @MappedSuperclass.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="exclude-unlisted-classes" type="xsd:boolean"

                           default="false" minOccurs="0">

                <xsd:annotation>

                  <xsd:documentation>

 

                    When set to true then only listed classes and jars will

                    be scanned for persistent classes, otherwise the enclosing

                    jar or directory will also be scanned. Not applicable to

                    Java SE persistence units.

 

                  </xsd:documentation>

                </xsd:annotation>

              </xsd:element>

 

              <!-- **************************************************** -->

 

              <xsd:element name="properties" minOccurs="0">

                <xsd:annotation>

                  <xsd:documentation>

 

                    A list of vendor-specific properties.

 

                  </xsd:documentation>

                </xsd:annotation>

                <xsd:complexType>

                  <xsd:sequence>

                    <xsd:element name="property"

                                 minOccurs="0" maxOccurs="unbounded">

                      <xsd:annotation>

                        <xsd:documentation>

                          A name-value pair.

                        </xsd:documentation>

                      </xsd:annotation>

                      <xsd:complexType>

                        <xsd:attribute name="name" type="xsd:string"

                                       use="required"/>

                        <xsd:attribute name="value" type="xsd:string"

                                       use="required"/>

                      </xsd:complexType>

                    </xsd:element>

                  </xsd:sequence>

                </xsd:complexType>

              </xsd:element>

 

            </xsd:sequence>

 

            <!-- **************************************************** -->

 

            <xsd:attribute name="name" type="xsd:string" use="required">

              <xsd:annotation>

                <xsd:documentation>

 

                  Name used in code to reference this persistence unit.

 

                </xsd:documentation>

              </xsd:annotation>

            </xsd:attribute>

 

            <!-- **************************************************** -->

 

            <xsd:attribute name="transaction-type"

                           type="persistence:persistence-unit-transaction-type">

              <xsd:annotation>

                <xsd:documentation>

 

                  Type of transactions used by EntityManagers from this

                  persistence unit.

 

                </xsd:documentation>

              </xsd:annotation>

            </xsd:attribute>

 

          </xsd:complexType>

        </xsd:element>

      </xsd:sequence>

      <xsd:attribute name="version" type="persistence:versionType"

                     fixed="1.0" use="required"/>

    </xsd:complexType>

  </xsd:element>

 

  <!-- **************************************************** -->

 

  <xsd:simpleType name="persistence-unit-transaction-type">

    <xsd:annotation>

      <xsd:documentation>

 

        public enum TransactionType { JTA, RESOURCE_LOCAL };

 

      </xsd:documentation>

    </xsd:annotation>

    <xsd:restriction base="xsd:token">

      <xsd:enumeration value=http://www.mamicode.com/"JTA"/>

      <xsd:enumeration value=http://www.mamicode.com/"RESOURCE_LOCAL"/>

    </xsd:restriction>

  </xsd:simpleType>

 

</xsd:schema>

 

 

jpa persistence.xml文件配置详解