Hibernate配置的纯JPA的代码移植到OpenJPA
注意:OpenJPA使用是 JPA2.0 规范,而Hibernate使用的 JPA2.1 规范。
- 问题:如果代码使用纯JPA标准编写。不修改代码只修改一下配置文件。是否可以将HIbernate的代码移植到其他的JPA框架?
- 答:可以的。只要学会了HIbernate的纯JPA的配置。那么就直接学会了OpenJPA和EclipseTOP等JPA框架。
注意:HIbernate是这么多JPA框架里面,性能最好的一个!
通过这个示例,可以证明只要符合JPA规范的代码,可以在基于JPA规则的框架相互切换。
不修改代码,只修改了配置文件。如下:
<?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://xmlns.jcp.org/xml/ns/persistence/persistence_2_0.xsd ">
<!-- persistence-unit标签:用于配置框架属性 name:唯一标识符,用于获得该标签下的信息 -->
<persistence-unit name="crm">
<!-- 加载映射实体类 -->
<!-- Hibernate版本的JPA已经实现了自动加载实体类,可以不配置 -->
<class>com.entity.Customer</class>
<!-- 配置环境参数 -->
<properties>
<!-- 四要素 -->
<!-- 属性在org.apache.openjpa.conf.OpenJPAConfigurationImpl中 -->
<!--
openjpa的环境参数,必须大写开头
openjpa. 可以写可以不写
-->
<property name="openjpa.ConnectionDriverName" value="org.gjt.mm.mysql.Driver" />
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/hibernate01" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="root" />
</properties>
</persistence-unit>
</persistence>
- 注意:OpenJPA使用是JPA2.0规则,而Hibernate使用的JPA2.1规范。
- 所以将Hibernate编写的JPA代码切换OpenJPA的代码,需要修改配置文件版本