我创建了一个非常简单的应用程序,使用持久化上下文(hibernate as provider)从数据库中读取一些值。我将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中。 请问,任何人都可以告诉我,在TomEE中如何看待pom.xml使用hibernate?
答案 0 :(得分:15)
<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/)。
使用范围为javaee-api
的{{1}} maven工件,您现在可以在项目中使用JPA规范。但是,如果您以前使用过某些特定于Hibernate的功能,类或注释,您仍然可以在provided
中引用Hibernate来匹配这些依赖项:
pom.xml
修改<!-- 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>
:
<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
既然您告诉TomEE如何建立连接,请在<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- Put <Resource> elements here -->
<resources>
中定义JTA数据源:
/src/main/java/META-INF/persistence.xml
在CDI bean或EJB中获取<?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>
,如下所示:
EntityManager
我正在使用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已包含@PersistenceContext(unitName = "my_persistence_unit")
private EntityManager em;
,因此您无需复制一个。
答案 1 :(得分:2)
试试这个:
添加:
<tomee-home>/lib/antlr-2.7.7.jar
<tomee-home>/lib/dom4j-1.6.1.jar
<tomee-home>/lib/ehcache-core-2.5.1.jar
<tomee-home>/lib/ehcache-terracotta-2.5.1.jar
<tomee-home>/lib/hibernate-commons-annotations-4.0.1.Final.jar
<tomee-home>/lib/hibernate-core-4.1.4.Final.jar
<tomee-home>/lib/hibernate-ehcache-4.1.4.Final.jar
<tomee-home>/lib/hibernate-entitymanager-4.1.4.Final.jar
<tomee-home>/lib/hibernate-validator-4.3.0.Final.jar
<tomee-home>/lib/jboss-logging-3.1.0.GA.jar
<tomee-home>/lib/terracotta-toolkit-1.4-runtime-4.1.0.jar
ehcache jar可能是可选的,但没有它们就没试过。
删除(可选):
<tomee-home>/lib/asm-3.2.jar
<tomee-home>/lib/bval-core-0.4.jar
<tomee-home>/lib/bval-jsr303-0.4.jar
<tomee-home>/lib/commons-lang-2.6.jar
<tomee-home>/lib/openjpa-2.2.0.jar
<tomee-home>/lib/serp-1.13.1.jar
答案 2 :(得分:0)
是的,只需将hibernate-jpa-2.1-api-1.0.0.Final.jar放入为我工作的TomEE lib文件夹中。