以前使用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注入到使用它的地方了。
分享到:
相关推荐
spring与jpa整合 除去persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库
最基础的JPA编程所需jar文件 附带mysql jar文件,persistence.xml配置文件
springMVC+JAP整合彻底摆脱persistence.xml配置文件 spring与jpa整合 除去persistence.xml配置文件 使用属性文件 数据源dbcp访问数据库.
The Java Persistence API (JPA) is the Java standard for mapping Java objects to a relational ...using an entity manager, creating and executing queries, and configuration of the persistence.xml file.
JPA 规范要求,配置文件必须命名为 persistence.xml,并存在于类路径下的 META-INF 目录中。该文件通常包含了初始化 JPA 引擎所需的全部信息。Spring 提供的 LocalContainerEntityManagerFactoryBean 提供了非常灵活...
通过参考和引用传智播客的免费教程,将springmvc4.1.6与hibernate4.3.10提供的JPA...7.注意web.xml,spring.xml,spring.xml,db.properties,persistence.xml文件的配置。 写了这么多,很辛苦,若对你有帮助,请给好评。
JPA连接MySQL的完整例子,创建了多个类,包括继承关系的类。包括persistence.xml的配制方法。
Spring mvc + JPA + mybatis.
13.1.3.2 配置文件persistence.xml......................... 4 13.1.3.3 实体类及标注.................................... 6 13.1.3.4 使用EntityManager来管理实体..................... 13 13.1.3.5 Query...
javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...
javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...
javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3号包: org.apache.log4j org.apache.openjpa org...
javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...
javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3号包: org.apache.log4j org.apache.openjpa org...
javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...
javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...
javax.xml.bind javax.xml.rpc javax.xml.soap javax.xml.stream javax.xml.ws net.sourceforge.cglib net.sourceforge.ehcache net.sourceforge.jasperreports net.sourceforge.jexcelapi net.sourceforge.jibx 3...
而是有一个sample-persistence.xml供您复制到 persistence.xml 并进行编辑。 假设您使用最好的数据库 (PostgreSQL),您只需更改 JDBC URL、用户名和密码。 如果您使用较小的数据库,则还必须更改 POM 文件。 此外,...
jpademo 该项目包含域模型的MusicRecording / VideoRecording部分,并且具有许多演示程序的Hibernate和JPA版本,包括著名... 只需更改persistence.xml中未注释掉的数据库参数集,就可以与H2,HSQLDB,MySQL和PostgresQ
使用Spring Boot无需任何XML文件的Spring Web MVC(+ Hibernate,Spring Data JPA)问候世界在此示例中,没有web.xml,没有Spring xml配置文件,没有persistence.xml,没有* .hbm.xml,只是没有XML配置文件,该文件...