坚持JBoss wildfly(在春季应用程序中)

时间:2013-12-19 10:20:37

标签: spring jpa persistence jboss7.x wildfly

我有一个spring 3.2应用程序,它使用以下配置定义了persisence:

<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="MYSQL"/>
    <property name="showSql" value="false"/>
    <property name="generateDdl" value="false"/>
    <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/>
</bean>

<util:map id="jpaPropertyMap">
        <entry key="hibernate.hbm2ddl.auto" value="update"/>
        <entry key="hibernate.cache.use_query_cache" value="true" />
        <entry key="hibernate.max_fetch_depth" value="4" />
        <entry key="hibernate.show_sql" value="false" />
        <entry key="hibernate.format_sql" value="false" />
        <entry key="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
        <entry key="hibernate.transaction.factory_class" value="org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory" />
        <entry key="hibernate.connection.autocommit" value="false" />
</util:map>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter"/>      
        <property name="jtaDataSource" ref="dataSource"  />
        <property name="jpaPropertyMap" ref="jpaPropertyMap" />
        <property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>

<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" >
        <property name="packagesToScan"  value="gr.hcg.nautologhsh.domain" />
        <property name="defaultJtaDataSource" ref="dataSource" />
</bean>

我的实体类存在于gr.hcg.nautologhsh.domain包下。我没有使用任何persistance.xml个文件。

现在,这种配置在JBoss 8 wildfly中运行良好。但是,当我看到JBoss的管理控制台时,在标签JPA中,我没有看到任何持久性单元。

从一些谷歌搜索我认为必须通过JBoss而不是通过Spring配置持久性单元以便在那里可见 - 这是真的吗?我真的不明白为什么JBoss中可能会有不同的PU - 它们如何被使用? JBoss的文档在该主题中非常简洁,我无法理解很多东西:(

有人可以向我解释如何定义JBoss持久性单元并在wildfly管理控制台的JPA标签中显示这些单元吗?

更新

我创建了一个persistence.xml,其中包含以下内容:

<persistence 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"
             version="2.0">

    <persistence-unit name="nautologhshPU" transaction-type="JTA">

        <jta-data-source>java:jboss/datasources/nautologhsh</jta-data-source>

    </persistence-unit>
</persistence>

改变了我的datasource-config.xml bean,如:

<jee:jndi-lookup  id="dataSource" jndi-name="java:jboss/datasources/nautologhsh" resource-ref="false" />

<tx:jta-transaction-manager  />

<jee:jndi-lookup jndi-name="java:comp/env/jpa/EntityManager" id="entityManager" expected-type="javax.persistence.EntityManager" />

并将以下内容添加到web.xml

<persistence-context-ref>
    <persistence-context-ref-name>jpa/EntityManager</persistence-context-ref-name>
    <persistence-unit-name>nautologhshPU</persistence-unit-name>
</persistence-context-ref>

现在出现以下错误:

20:24:41,839 ERROR [org.jboss.msc.service.fail] (ServerService Thread Pool -- 52) MSC000001: Failed to start service jboss.persistenceunit."Nautologhsh.war#nautologhshPU": org.jboss.msc.service.StartException in service jboss.persistenceunit."Nautologhsh.war#nautologhshPU": org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]
        at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:169) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
        at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:117) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
        at java.security.AccessController.doPrivileged(Native Method) [rt.jar:1.7.0_45]
        at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:463) [wildfly-security-manager-1.0.0.Beta3.jar:1.0.0.Beta3]
        at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:178) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
        at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
        at org.jboss.threads.JBossThread.run(JBossThread.java:122) [jboss-threads-2.1.1.Final.jar:2.1.1.Final]
Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]
        at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:127)
        at org.hibernate.envers.event.EnversIntegrator.integrate(EnversIntegrator.java:65)
        at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:310)
        at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1837)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:854)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:847)
        at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:396)
        at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:846)
        at org.jboss.as.jpa.hibernate4.TwoPhaseBootstrapImpl.build(TwoPhaseBootstrapImpl.java:44)
        at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1$1.run(PersistenceUnitServiceImpl.java:151) [wildfly-jpa-8.0.0.Beta1.jar:8.0.0.Beta1]
        ... 8 more

20:24:41,861 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("deploy") failed - address: ([("deployment" => "Nautologhsh.war")]) - failure description: {"JBAS014671: Failed services" => {"jboss.persistenceunit.\"Nautologhsh.war#nautologhshPU\"" => "org.jboss.msc.service.StartException in service jboss.persistenceunit.\"Nautologhsh.war#nautologhshPU\": org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]
    Caused by: org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService]"}}

是否有人知道这是什么org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService] ??

1 个答案:

答案 0 :(得分:3)

您可以查看此快速入门示例:https://github.com/sgilda/wildfly-quickstart/blob/master/greeter-spring使用META-INF/persistence.xml文件,其中Spring配置使用查找从Wildfly检索EntityManager和Datasource:

<!-- Look up the database in JNDI -->
<jee:jndi-lookup jndi-name="java:jboss/datasources/ExampleDS" id="dataSource"
  expected-type="javax.sql.DataSource" />

<!-- Look up the container deployed EntityManager -->
<jee:jndi-lookup jndi-name="java:comp/env/hello/EntityManager"
  id="entityManager" expected-type="javax.persistence.EntityManager" />

https://github.com/sgilda/wildfly-quickstart/blob/master/greeter-spring/src/main/webapp/WEB-INF/spring-business-context.xml