java 如何在 Hibernate 中使用 TomEE
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10852035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use TomEE with Hibernate
提问by LancerX
I have created very simple app with persistence context (hibernate as provider) to read some value from database. I use Eclipse with Maven.
我创建了一个非常简单的应用程序,它带有持久性上下文(休眠作为提供者)来从数据库中读取一些值。我将 Eclipse 与 Maven 一起使用。
First, I get
首先,我得到
Caused by: org.apache.openejb.OpenEJBException: java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence cannot be cast to javax.persistence.spi.PersistenceProvider:
and according to this topic http://openejb.979440.n4.nabble.com/problem-with-hibernate-persistence-provider-td980429.htmlI excluded hibernate-jpa-2.0-api. Now, my dependencies look
根据这个话题 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>
Now, I don't know why...
现在,我不知道为什么...
Caused by: java.lang.ClassNotFoundException: org.hibernate.transaction.TransactionManagerLookup
But TransactionManagerLookup is in hibernate-core. Please, can anybody tell me, how should look pom.xml to use hibernate in TomEE?
但是 TransactionManagerLookup 在休眠核心。拜托,有人可以告诉我,应该如何查看 pom.xml 以在 TomEE 中使用 hibernate?
采纳答案by David Blevins
Try this:
试试这个:
Add:
添加:
<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
<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
The ehcache jars might be optional, but haven't tried without them.
ehcache jars 可能是可选的,但没有尝试过。
Remove (optional):
删除(可选):
<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
<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
回答by Hyman
1. Copy the required Hibernate .jars to <tomee-home>/lib
1. 将所需的 Hibernate .jars 复制到 <tomee-home>/lib
According to the documentation ( http://tomee.apache.org/tomee-and-hibernate.html), the following ones are sufficient and in fact they worked for me:
根据文档(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
All these .jars
are contained in the Hibernate ORM 4.2.x download ( http://hibernate.org/orm/), except for the Hibernate Validator, which is a separate download ( http://hibernate.org/validator/).
所有这些.jars
都包含在 Hibernate ORM 4.2.x 下载 ( http://hibernate.org/orm/) 中,除了 Hibernate Validator,它是一个单独的下载 ( http://hibernate.org/validator/)。
2. Edit your pom.xml
2. 编辑你的 pom.xml
Using the javaee-api
maven artifact with a scope of provided
you can now use the JPA specification in your project. However, if you have been using some Hibernate specific features, classes or annotations before, you can still refer to Hibernate in your pom.xml
to match those dependencies:
在javaee-api
一定范围内使用maven 工件provided
现在可以在您的项目中使用 JPA 规范。然而,如果你之前使用过一些 Hibernate 特定的特性、类或注解,你仍然可以在你的文件中引用 Hibernatepom.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>
3. Define your database connection
3. 定义你的数据库连接
Edit <tomee-home>/conf/tomee.xml
:
编辑<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>
You can also put the above <Resource>...</Resource>
definition into WEB-INF/resources.xml
and ship it with your application instead:
您还可以将上述<Resource>...</Resource>
定义放入WEB-INF/resources.xml
并随您的应用程序一起提供:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<!-- Put <Resource> elements here -->
<resources>
4. JTA Datasource
4. JTA 数据源
Now that you told TomEE how to establish a connection, define a JTA datasource in /src/main/java/META-INF/persistence.xml
:
既然您已告诉 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. Start using JPA
5.开始使用JPA
Obtain an EntityManager
in a CDI bean or EJB like this:
EntityManager
像这样在 CDI bean 或 EJB 中获取一个:
@PersistenceContext(unitName = "my_persistence_unit")
private EntityManager em;
Final Notes
最后的笔记
Hibernate versions 4.3+
休眠版本 4.3+
I am using Hibernate 4.2.21 (JPA 2.0, Java EE 6) along with TomEE 1.7.2. Any TomEE 1.7.x, 1.6.x and 1.5.x will work. However, you cannot use Hibernate 4.3+ (JPA 2.1 / Java EE 7), as TomEE 1.7.x and below only support Java EE 6. If you really want to use Java EE 7 features along with TomEE, this blog post might be helpful: http://rmannibucau.wordpress.com/2013/07/19/little-tip-to-help-you-to-test-javaee-7-in-tomee-with-tomee-maven-plugin/
我正在使用 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
TomEE 1.5.x already includes a javassist-<version>.jar
, so you don't have to copy one.
TomEE 1.5.x 已经包含一个javassist-<version>.jar
,因此您不必复制一个。
回答by Abdul Ahad
yes just dropping the hibernate-jpa-2.1-api-1.0.0.Final.jar into the TomEE lib folder worked for me.
是的,只是将 hibernate-jpa-2.1-api-1.0.0.Final.jar 放入对我有用的 TomEE lib 文件夹中。