2

I have a spring 3.2 application which has defined persisence with the following configuration:

<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>

My entity classes exist under the gr.hcg.nautologhsh.domain package. I am not using any persistance.xml files.

Now, this configuration is working fine in JBoss 8 wildfly. However when I see the Administration console of JBoss, in the tag JPA, I don't see any persistance units.

From some googling I think that the persistence units have to be configured through JBoss and not through Spring in order to be visible there - is that true ? I can't really understand why there could be different PUs in JBoss - how can they be used ? The documentation of JBoss is very spartian in that subject and I couldn't understand many things :(

Can somebody explain to me how can I define JBoss persistence units and show these in the JPA tag of wildfly's administration console ?

Update

I created a persistence.xml with the following contents:

<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>

changed my datasource-config.xml beans like ths:

<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" />

and added the following to 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>

And now get the following error:

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]"}}

Does any body know what is this org.hibernate.service.UnknownServiceException: Unknown service requested [org.hibernate.service.classloading.spi.ClassLoaderService] ??

Serafeim
  • 14,962
  • 14
  • 91
  • 133

1 Answers1

3

You can take a look at this quickstart example: https://github.com/sgilda/wildfly-quickstart/blob/master/greeter-spring which uses a META-INF/persistence.xml file an in which Spring configuration uses lookups to retrieve the EntityManager and the Datasource from Wildfly:

<!-- 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)

Joshua Davis
  • 3,499
  • 1
  • 26
  • 29
Xavier Coulon
  • 1,580
  • 10
  • 15
  • This is a nice example! I see that there is a container deployed EntityManager with a ``jndi-name="java:comp/env/hello/EntityManager"``. However I can't really understand *where* is this EntityManager defined ? I mean how do we know that our entity manager has this jndi name (hello/EntityManager)? – Serafeim Dec 19 '13 at 13:17
  • The EntityManager is provided by Wildfly itself (it already comes with Hibernate). So you just have to use inject it in your Spring beans, but it's not your Spring application's duty to initialize it ;-) – Xavier Coulon Dec 19 '13 at 13:23
  • Yes I understand that - what I didn't get was how did you know that the JNDI name of the EntityManager is actually ``hello/EntityManager`` ? – Serafeim Dec 19 '13 at 13:30
  • Sorry, I misread your question. Good point actually: the binding is declared in the web.xml deployment descriptor: ` hello/EntityManager helloPU ` – Xavier Coulon Dec 19 '13 at 14:44
  • Can you take a look at my updated question ? Thanks ! – Serafeim Dec 19 '13 at 18:26
  • The link to the example on Github is broken. https://github.com/wildfly/quickstart/blob/master/greeter-spring/src/main/webapp/WEB-INF/spring-business-context.xml <= 404 – Joshua Davis Dec 29 '14 at 15:59
  • Ah, found it: https://github.com/sgilda/wildfly-quickstart/blob/master/greeter-spring/src/main/webapp/WEB-INF/spring-business-context.xml – Joshua Davis Jan 09 '15 at 17:06