openJPA的persistence.xml配置

时间:2011-12-09 14:18:53

标签: openjpa

在我的项目早期,我使用了Hibernate 3.3.2,openJPA 2.1.1的组合来连接数据库并从表中检索信息。现在我想删除Hibernate并使用openJPA进行连接并检索信息。

我之前的persistence.xml配置是

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
        <property name="hibernate.connection.url" value="jdbc:mysql://10.10.10.10:3306/test?autoReconnect=true"/>
        <property name="hibernate.connection.username" value="user"/>
        <property name="hibernate.connection.password" value="pwd"/>
        <property name="hibernate.generate_statistics" value="true"/>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.OSCacheProvider"/>
        <property name="hibernate.cache.use_second_level_cache" value="true"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.format_sql" value="true"/>
        <property name="hibernate.c3p0.min_size" value="5"/>
        <property name="hibernate.c3p0.max_size" value="20"/>
        <property name="hibernate.c3p0.timeout" value="1800"/>
        <property name="hibernate.c3p0.max_statements" value="50"/>
        <property name="hibernate.c3p0.idle_test_period" value="1800"/>
        <property name="c3p0.idleConnectionTestPeriod" value="1810"/>
      </properties>
   </persistence-unit>
</persistence>

现在我想转到openJPA 2.1.1,我的persistence.xml文件是

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
<persistence-unit name="TestOpenJPAPersistence" transaction-type="RESOURCE_LOCAL">
    <provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
    <properties>
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://10.10.10.10:3306/test?autoReconnect=true"/>
        <property name="javax.persistence.jdbc.user" value="user"/>
        <property name="javax.persistence.jdbc.password" value="pwd"/>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
        <property name="openjpa.ConnectionFactoryProperties" value="MaxActive=10,MaxIdle=5,MinIdle=2,MaxWait=1800000"/>
        <property name="openjpa.Log" value="File=E:\\temp\\TestOpenJPAPersistence\\org.apache.openjpa.log, DefaultLevel=DEBUG, Runtime=INFO, Tool=INFO, SQL=TRACE"/>
        <property name="openjpa.jdbc.DBDictionary" value="org.apache.openjpa.jdbc.sql.MySQLDictionary"/>
        <property name="openjpa.DataCache" value="true"/>
        <property name="openjpa.QueryCache" value="true"/>
    </properties>
</persistence-unit>
</persistence>

我的问题是在openJPA中跟随hibernate属性的替换属性是什么

<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.OSCacheProvider"/>
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="1800"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="1800"/>

1 个答案:

答案 0 :(得分:10)

经过多次试验和错误后,我找到了一些替换参数。我现在的persistence.xml文件看起来像

persistence.xml:

<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<properties>
  <property name="openjpa.ConnectionURL" value="jdbc:mysql://xx.xx.xx.xx:3306/dbname"/>
  <property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
  <property name="openjpa.ConnectionUserName" value="xxx"/>
  <property name="openjpa.ConnectionPassword" value="xxx"/>
  <property name="openjpa.DynamicEnhancementAgent" value="true"/>
  <property name="openjpa.RuntimeUnenhancedClasses" value="supported"/>
  <property name="openjpa.Log" value="SQL=TRACE"/>
  <property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72, PrintParameters=true, MaxActive=10, MaxIdle=5, MinIdle=2, MaxWait=60000"/>
</properties>

用于连接池信息check以及所有OpenJPA属性check

希望这有帮助