以前使用JPA的实现是toplink,现在改为hibernate,所以要修改persistence.xml文件,两者的配置有一些不一样,并且在EE环境下面和SE的环境下面也有不一样,还有一点,那就是当persistence.xml里面有些格式出错的时候,虽然出错的不是我们需要的那个单元,但也会使得整个persistence.xml报废。
下面帖的是在SE的环境下面使用toplink和hibernate的实现,两者都写在同一个persistence.xml里面。这样切换起来也方便一些。
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.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_1_0.xsd"> <persistence-unit name="TestSSH2PU" transaction-type="RESOURCE_LOCAL"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <class>com.hadeslee.jpaentity.Department</class> <class>com.hadeslee.jpaentity.Person</class> <properties> <property name="toplink.jdbc.user" value="sa"/> <property name="toplink.jdbc.password" value="hadeslee"/> <property name="toplink.jdbc.url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"/> <property name="toplink.jdbc.driver" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="toplink.ddl-generation" value="create-tables"/> </properties> </persistence-unit> <persistence-unit name="TestSSH1PU2" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>com.hadeslee.jpaentity.Department</class> <class>com.hadeslee.jpaentity.Person</class> <properties> <property name="hibernate.connection.driver_class" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/> <property name="hibernate.connection.url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testSSH"></property> <property name="hibernate.connection.username" value="sa"></property> <property name="hibernate.connection.password" value="hadeslee"></property> <property name="hibernate.show_sql" value="true"></property> <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"></property> <property name="hibernate.current_session_context_class" value="thread"></property> </properties> </persistence-unit> </persistence>
在SE的环境下面,是不能使用容器的JTA的数据源的。并且不能使用
<exclude-unlisted-classes>true</exclude-unlisted-classes>这个属性。
本文重点是记录下两个常用的JPA的实现的配置。目前是在SE环境下的配置。EE环境下面的配置如下:
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.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_1_0.xsd"> <persistence-unit name="unit_mssql" transaction-type="JTA"> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <jta-data-source>MobileOAMSSQL</jta-data-source> <properties> <property name="toplink.ddl-generation" value="create-tables"/> <property name="toplink.logging.level" value="FINE"/> </properties> </persistence-unit> <persistence-unit name="MyApp-ejbPU2" transaction-type="JTA"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>MobileOAMYSQL</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> </properties> </persistence-unit> </persistence>
在EE环境下面使用JPA配置就简单了许多,首先他可以把当前模块的CLASS文件都包括进来,不用手工指定。并且也少了很多有关于数据库连接的操作,因为这个时候都是从容器里面去取数据源的。并且此时的事务是由容器去管理的,也就是使用JTA,不再是RESOURCE_LOCAL了。这样在代码里面就不用em.getTransaction().begin();和em.getTransaction().commit()了,并且可以使用注入功能,把EntityManager注入到使用它的地方了。
来自于http://spiritfrog.iteye.com/blog/551135
配置说明:
<?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>
实例:
<?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="EjbXmlMappingsPU" // 持久化单元名 transaction-type="JTA(default)/RESOURCE_LOCAL" // 事务类型 --> <persistence-unit name="EjbXmlMappingsPU" transaction-type="JTA"> <description></description> <!-- 描述 --> <provider>oracle.toplink.essentials.PersistenceProvider</provider> <!-- 指定一个持久化提供者 --> <jta-data-source>jdbc/MyDataSource</jta-data-source> <!-- 指定JTA数据源 --> <non-jta-data-source>jdbc/NonTxMyDataSource</non-jta-data-source> <!-- 指定非JTA数据源 --> <mapping-file>META-INF/my_queries.xml</mapping-file> <!-- 指定映射文件 --> <mapping-file>META-INF/my_entities.xml</mapping-file> <jar-file>my/my-classes.jar</jar-file> <!-- 指定托管类的附加JAR --> <class>myclass1</class> <!-- 明确列出的类 --> <class>myclass2</class> <exclude-unlisted-classes/> <!-- 使本地类不能被加到持久化单元中 --> <properties> <!-- 使用提供者属性 --> <property name="toplink.logging.level" value="FINE"/> <property name="toplink.cache.size.dafault" value="500"/> </properties> </persistence-unit> </persistence>