4 Replies Latest reply on May 4, 2014 4:37 AM by malpi

    Howto JBoss AS7 and Spring Data JPA

    malpi

      Hi there,

       

      i am trying to get an application running which is using JBoss as application server. Furthermore I want to use Spring Data JPA without the Spring Application Context, so lets say plain JEE.

       

      Spring provides a CDI extension which should natively work. But I am always getting the following exception.

       

      javax.enterprise.inject.UnsatisfiedResolutionException: Unable to resolve a bean for 'javax.persistence.EntityManager' with qualifiers [@javax.enterprise.inject.Any(), @javax.enterprise.inject.Default()].

          at org.springframework.data.jpa.repository.cdi.JpaRepositoryExtension.createRepositoryBean(JpaRepositoryExtension.java:112)

          at org.springframework.data.jpa.repository.cdi.JpaRepositoryExtension.afterBeanDiscovery(JpaRepositoryExtension.java:91)

      I followed the spring reference guide, which state to create an Entitymanger producer. (http://docs.spring.io/spring-data/jpa/docs/1.4.2.RELEASE/reference/html/jpa.repositories.html#jpd.misc.cdi-integration)

       

      @Produces
          @ApplicationScoped
          public EntityManagerFactory createEntityManagerFactory() {
              return Persistence.createEntityManagerFactory("persistenceUnit");
          }
      
          public void close(@Disposes EntityManagerFactory entityManagerFactory) {
              entityManagerFactory.close();
          }
      
      

       

      But still the exception stays..

      My persistence.xml looks like the following:

       

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
          xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
          http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
      
          <persistence-unit name="persistenceUnit" transaction-type="JTA">
              <jta-data-source>java:/myDS</jta-data-source>
              <class>de.company.project.client.domain.User</class>
                <properties>
                  <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
                  <!-- value="create" to build a new database on each run; value="update" 
                      to modify an existing database; value="create-drop" means the same as "create" 
                      but also drops tables when Hibernate closes; value="validate" makes no changes 
                      to the database -->
                  <property name="hibernate.hbm2ddl.auto" value="create" />
                  <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
                  <property name="hibernate.connection.charSet" value="UTF-8" />
                  <property name="hibernate.default_schema" value="public" />
                  <property name="hibernate.show_sql" value="false" />
                  <property name="jboss.entity.manager.factory.jndi.name" value="java:/ttsEntityManagerFactory"/> 
              </properties>
      
          </persistence-unit>
      
      </persistence>
      

       

      I only put line 22 in there because it was stated in the JBOSS documentation, but it didn't change anything.

      Somebody has an idea how to solve the exception? Or knows an example?

       

      Thanks in advance,

       

      Malte

        • 1. Re: Howto JBoss AS7 and Spring Data JPA
          kipriz

          The only way I found is to declare entity manager factory and entity manager itself in the following way:

           

          public class CdiConfig {
              @Produces
              @Dependent
              @PersistenceUnit
              private EntityManagerFactory entityManagerFactory;
          
              @Produces
              @Dependent
              public EntityManager createEntityManager(EntityManagerFactory emf) {
                  return emf.createEntityManager();
              }
          
              public void close(@Disposes EntityManager entityManager) {
                  entityManager.close();
              }
          }
          

           

          I'm not sure of behavior of this @Dependent scope and how it will behave: memory leaks, performance etc. However, I was able to run and use Spring Data Jpa repositories with this configuration and Jboss EAP 6.2.

          • 2. Re: Howto JBoss AS7 and Spring Data JPA
            thang81

            thanks, your CdiConfig save me a day.

             

            this could run well with Arquillian test, jboss 6.2

            • 3. Re: Howto JBoss AS7 and Spring Data JPA
              kipriz

              I found another configuration that works better and correct:

              public class CdiConfig {
                  @Produces
                  @Dependent
                  @PersistenceContext
                  private EntityManager entityManager;
              }
              
              • 4. Re: Howto JBoss AS7 and Spring Data JPA
                malpi

                Hi there,

                 

                In the meantime I already found a solution and I concluded them in a blogpost.

                 

                http://mpickhan.de/2014/05/03/howto-use-spring-data-jpa-with-errai/

                 

                Best Regards,

                 

                Malte

                1 of 1 people found this helpful