Persistance in JBoss wildfly (in a spring application)
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" />
Serafeim
Serafeim S. Papastefanos received his Diploma (2005) and Phd (2010) in EE from the National Technical University of Athens. He has been awarded for his academic progress from State Scholarships Foundation (SSF). His research interests include network management, network protocols, video coding, video indexing and video streaming. Since then he is working as a Software Engineer, starting from automating business processes with the Appian platform (heavy usage of Java/Javascript) and continuing with Django web development. He also keeps a small blog about various programming stuff @ http://spapas.github.io/ SOreadytohelp
Updated on June 13, 2022Comments
-
Serafeim almost 2 years
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]
??