在Glassfish 4(范例)上使用Hibernate 4.3 .x

・3 分钟阅读

这将允许您使用Java EE 7上的新Hibernate框架升级到JPA 2.1,并使用标准JPA EntityManager维护您的JTA数据源。

首先你需要从http://hibernate.org/orm/downloads/下载Hibernate 4.3 .x,并将所有需要的jar放入,{Glassfish4 direcory}/glassfish/lib
我还安装了jboss-logging-3.1.x.GA.jar{Glassfish4 direcory}/glassfish/lib/endorsed 允许Hibernate日志记录的文件夹。

我建议对项目依赖项管理使用Maven,因为Hibernate具有较大的依赖关系树。 将以下依赖项添加到pom.xml


<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>4.3.0.Final</version>
</dependency>

在 Hibenate 3.6和JPA 2.0, 你的persistence.xml文件将如下所示:


<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="myunitname" transaction-type="JTA">
 <provider>org.hibernate.ejb.HibernatePersistence</provider>
 ...
 </persistence-unit>
</persistence>

现在使用Hibernate 4.3,你必须更新你的persitence版本和provider类:


<persistence version="2.1" 
 xmlns="http://xmlns.jcp.org/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/persistence/persistence_2_1.xsd">
 <persistence-unit name="myunitname" transaction-type="JTA">
 <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
 <jta-data-source>jdbc/my-data-source</jta-data-source>
 <properties>
 <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform" />
 ...
 </properties>
 </persistence-unit>
</persistence>

注意添加属性hibernate.transaction.jta.platform是必需的,值将根据容器的不同而且改变,对于Glassfish来说 org.hibernate.service.jta.platform.internal.SunOneJtaPlatform 有关其他平台实现,请参见这里: http://docs.jboss.org/hibernate/core/4.0/javadocs/org/hibernate/service/jta/platform/internal/package-summary.html

Xn_warm profile image