首页 > 代码库 > Jpa规范中persistence.xml 配置文件解析

Jpa规范中persistence.xml 配置文件解析

数据库已经配好的前提下 persistence.xml配置如下
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="persistenceUnitWrite" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <non-jta-data-source>dataSource</non-jta-data-source>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
            <property name="hibernate.jdbc.use_streams_for_binary" value="false"/>
            <!-- value="http://www.mamicode.com/create" to build a new database on each run; value="http://www.mamicode.com/update" to modify an existing database; value="http://www.mamicode.com/create-drop" means the same as "create" but also drops tables when Hibernate closes; value="http://www.mamicode.com/validate" makes no changes to the database -->

            <!-- <property name="hibernate.hbm2ddl.auto" value="http://www.mamicode.com/validate"/> -->
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy"/>
            <property name="hibernate.connection.charSet" value="UTF-8"/>
            <property name="hibernate.show_sql" value="false" />
            <property name="hibernate.format_sql" value="true" />

            <property name="hibernate.order_updates" value="true" />

            <!-- 
         <property name="hibernate.cache.use_second_level_cache" value="http://www.mamicode.com/true"/>
         <property name="hibernate.cache.provider_class" value ="http://www.mamicode.com/net.sf.ehcache.hibernate.EhCacheProvider"/>
         http://ehcache.org/documentation/2.4/user-guide/hibernate#Downloading-and-Installing-Ehcache
             -->
            
            <!-- Uncomment the following two properties for JBoss only -->
            <!-- property name="hibernate.validator.apply_to_ddl" value="http://www.mamicode.com/false" /-->
            <!-- property name="hibernate.validator.autoregister_listeners" value="http://www.mamicode.com/false" /-->
        </properties>
    </persistence-unit>


</persistence>

 





使用spring data + hibernate 进行逻辑层操作时候需要配置 persistence.xml的内容
<?xml version="1.0"?> 

<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/persistence_1_0.xsd" version="1.0"> 

<persistence-unit name="itcast" transaction-type="RESOURCE_LOCAL"> 

<properties> 
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" /> 
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysqlDriver" /> 
<property name="hibernate.connection.username" value="root" /> 
<property name="hibernate.connection.password" value="root" /> 
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/testdatabase?useUnicode=true&characterEncoding=UTF-8" /> 
<property name="hibernate.max_fetch_depth" value="3" /> 
<property name="hibernate.show_sql" value="true" /> 
<property name="hibernate.hbm2ddl.auto" value="create-drop"/> 
</properties> 

</persistence-unit>

 

注释

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

<persistence version="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-unit name="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 --> 
<property name="hibernate.hbm2ddl.auto" value="update" /> 
<property name="hibernate.show_sql" value="true" /> 
</properties> 

</persistence-unit> 

</persistence>

 

Jpa规范中persistence.xml 配置文件解析