持久化部署问题

发布于 2024-09-15 18:05:48 字数 2474 浏览 4 评论 0原文

我有一个 hibernate 项目,它使用 JPA。

我的 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="Demo-PU" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/DemoDS</non-jta-data-source> 
    <class>com.demo.framework.entity.ReportDefinitionEntity</class> 
    <properties>

<!--  Database connection -->
  <property name="hibernate.connection.url" value="jdbc:mysql://192.168.9.110:3306/demoDB" />
  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
  <property name="hibernate.connection.username" value="root" />
  <property name="hibernate.connection.password" value="root" />

 <!--  Hibernate dialect  -->
  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!--  Output goodies
  -->
  <property name="hibernate.query.jpaql_strict_compliance" value="true" />
  <property name="hibernate.format_sql" value="true" />
  <property name="hibernate.use_sql_comments" value="false" />
   <!--  Cache
  -->
  <property name="hibernate.jdbc.batch_versioned_data" value="true" />
  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />

    </properties>
  </persistence-unit>
</persistence>

现在,当我使用 eclipse 运行它时,我没有问题,但是当我在 Jboss 中部署它时,出现以下错误:

错误[AbstractKernelController]错误 安装到开始: name=persistence.unit:unitName=#Demo-PU 状态=创建 java.lang.ClassCastException: org.hibernate.ejb.HibernatePersistence 无法投射到 javax.persistence.spi.PersistenceProvider

这是我拥有的 Jar 列表

activation.jar
antlr-2.7.6.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-tools.jar
hibernate3.jar
javassist.jar
javax.persistence.jar
jdbc2_0-stdext.jar
jta.jar
mysql-connector-java-5.0.5-bin.jar
xml-writer.jar

如何解决此问题?

I have a hibernate project, which uses JPA.

my persistence.xml contents is as follows:

<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="Demo-PU" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <non-jta-data-source>java:/DemoDS</non-jta-data-source> 
    <class>com.demo.framework.entity.ReportDefinitionEntity</class> 
    <properties>

<!--  Database connection -->
  <property name="hibernate.connection.url" value="jdbc:mysql://192.168.9.110:3306/demoDB" />
  <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
  <property name="hibernate.connection.username" value="root" />
  <property name="hibernate.connection.password" value="root" />

 <!--  Hibernate dialect  -->
  <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<!--  Output goodies
  -->
  <property name="hibernate.query.jpaql_strict_compliance" value="true" />
  <property name="hibernate.format_sql" value="true" />
  <property name="hibernate.use_sql_comments" value="false" />
   <!--  Cache
  -->
  <property name="hibernate.jdbc.batch_versioned_data" value="true" />
  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider" />

    </properties>
  </persistence-unit>
</persistence>

Now when I run it using eclipse I don't have a problem, but when I deploy it in Jboss, I get the below error:

ERROR [AbstractKernelController] Error
installing to Start:
name=persistence.unit:unitName=#Demo-PU
state=Create
java.lang.ClassCastException:
org.hibernate.ejb.HibernatePersistence
cannot be cast to
javax.persistence.spi.PersistenceProvider

And here is the list of Jar that I have

activation.jar
antlr-2.7.6.jar
asm-attrs.jar
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.1.jar
dom4j-1.6.1.jar
ehcache-1.2.3.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate-entitymanager.jar
hibernate-tools.jar
hibernate3.jar
javassist.jar
javax.persistence.jar
jdbc2_0-stdext.jar
jta.jar
mysql-connector-java-5.0.5-bin.jar
xml-writer.jar

How can I resolve this issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满地尘埃落定 2024-09-22 18:05:48

ClassCastException 是由于您的系统中存在两份 javax.persistence API 副本(一份位于 JBoss 提供的公共类加载器中,一份位于您的应用程序中)引起的。当在 JBoss 上运行时,您不应该在应用程序中提供此 API,也不要打包它。

顺便说一句,您似乎正在使用 JPA 2.0 persistence.xml 但我不相信您正在使用 Hibernate 的 JPA 2.0 实现(实际上,您似乎正在使用一个相当旧的版本,因为我可以看到 commons-logging.jar)。您可能应该修复该问题,即使用 1.0 版本的 persistence.xml

实际上,在 JBoss 上运行时您很可能应该使用不同的persistence.xml(使用 JTA 实体管理器和 jta-data-source)。而且混合使用数据源和 Hibernate 内置连接池似乎很奇怪。

The ClassCastException is caused by having two copies of the javax.persistence APIs in your system (one in the common classloader provided by JBoss and the one in your app). When running on JBoss, you are just not supposed to provide this API in your application, don't package it.

By the way, it seems you're using a JPA 2.0 persistence.xml but I'm not convinced you're using the JPA 2.0 implemenation of Hibernate (actually, you seem to be using a pretty old version since I can see commons-logging.jar). You should probably fix that i.e. use the 1.0 version of persistence.xml.

Actually, you should very likely use a different persistence.xml when running on JBoss (using a JTA entity manager and a jta-data-source). And it seems weird to mix data source usage and Hibernate built-in connection pool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文