gpt4 book ai didi

java - 如何在 Hibernate 中使用 TomEE

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:09:41 26 4
gpt4 key购买 nike

我已经创建了非常简单的应用程序,它具有持久性上下文( hibernate 作为提供者)以从数据库中读取一些值。我将 Eclipse 与 Maven 结合使用。

首先,我得到

Caused by: org.apache.openejb.OpenEJBException: java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence cannot be cast to javax.persistence.spi.PersistenceProvider:

然后根据这个话题 http://openejb.979440.n4.nabble.com/problem-with-hibernate-persistence-provider-td980429.html我排除了 hibernate-jpa-2.0-api。现在,我的依赖项看起来

<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.3.Final</version>
<exclusions>
<exclusion>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
</exclusion>
</exclusions>
</dependency>

现在,我不知道为什么...

Caused by: java.lang.ClassNotFoundException: org.hibernate.transaction.TransactionManagerLookup

但是 TransactionManagerLookup 在 hibernate-core 中。拜托,谁能告诉我,应该如何查看 pom.xml 才能在 TomEE 中使用 hibernate ?

最佳答案

1。将所需的 Hibernate .jars 复制到 <tomee-home>/lib

根据文档(http://tomee.apache.org/tomee-and-hibernate.html),以下内容就足够了,事实上它们对我有用:

<tomee-home>/lib/antlr-2.7.7.jar
<tomee-home>/lib/dom4j-1.6.1.jar
<tomee-home>/lib/hibernate-commons-annotations-4.0.2.Final.jar
<tomee-home>/lib/hibernate-core-4.2.21.Final.jar
<tomee-home>/lib/hibernate-entitymanager-4.2.21.Final.jar
<tomee-home>/lib/hibernate-validator-4.3.2.Final.jar
<tomee-home>/lib/javassist-3.18.1-GA.jar
<tomee-home>/lib/jboss-logging-3.1.0.GA.jar

所有这些 .jars包含在 Hibernate ORM 4.2.x 下载 ( http://hibernate.org/orm/ ) 中,Hibernate Validator 除外,它是一个单独的下载 ( http://hibernate.org/validator/ )。

2。编辑你的 pom.xml

使用 javaee-api范围为 provided 的 Maven 工件您现在可以在您的项目中使用 JPA 规范。但是,如果您以前一直在使用一些 Hibernate 特定的功能、类或注释,您仍然可以在 pom.xml 中引用 Hibernate。匹配这些依赖项:

<!-- JPA spec (required) -->
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0-4</version>
<scope>provided</scope>
</dependency>
<!-- Hibernate specific features (only if needed) -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.21.Final</version>
<scope>provided</scope>
</dependency>

3。定义你的数据库连接

编辑 <tomee-home>/conf/tomee.xml :

<Resource id="myJtaDatabase" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://localhost:3306/my_dbname?autoReconnect=true
UserName foo
Password bar
validationQuery = SELECT 1
JtaManaged true
</Resource>

也可以把上面的<Resource>...</Resource>定义为 WEB-INF/resources.xml并将其与您的应用程序一起发送:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- Put <Resource> elements here -->
<resources>

4。 JTA数据源

既然您已告诉 TomEE 如何建立连接,请在 /src/main/java/META-INF/persistence.xml 中定义一个 JTA 数据源。 :

<?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="my_persistence_unit">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:openejb/Resource/myJtaDatabase</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<!-- As many hibernate properties as you need, some examples: -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<!-- Drop and then re-create the database schema (don't do this in production) -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>

5。开始使用 JPA

获得 EntityManager在像这样的 CDI bean 或 EJB 中:

@PersistenceContext(unitName = "my_persistence_unit")
private EntityManager em;

最后的笔记

hibernate 版本 4.3+

我正在使用 Hibernate 4.2.21(JPA 2.0、Java EE 6)和 TomEE 1.7.2。任何 TomEE 1.7.x、1.6.x 和 1.5.x 都可以使用。但是,您不能使用 Hibernate 4.3+ (JPA 2.1/Java EE 7),因为 TomEE 1.7.x 及以下版本仅支持 Java EE 6。如果您真的想将 Java EE 7 功能与 TomEE 一起使用,这篇博文可能会有所帮助: http://rmannibucau.wordpress.com/2013/07/19/little-tip-to-help-you-to-test-javaee-7-in-tomee-with-tomee-maven-plugin/

TomEE 1.5.x

TomEE 1.5.x 已经包含一个 javassist-<version>.jar ,所以您不必复制一个。

关于java - 如何在 Hibernate 中使用 TomEE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10852035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com