title: Spring Data JPA + Hibernate + EJB on Wildfly 10
body: <p>I'm developing a Web application using JavaEE stack (Hibernate + EJB3 + PrimeFaces) but for DAOs, I'm planing to use Spring Data JPA.</p>
  
  <p>To get started I have, some questions:</p>
  
  <p>1) Is it possible to use ONLY spring-data-jpa?</p>
  
  <p>2) How do I load applicationContext.xml?</p>
  
  <p>What I tried until now:</p>
  
  <p>1) Created /META-INF/applicationContext.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:c="http://www.springframework.org/schema/c"
          xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
          xmlns:jpa="http://www.springframework.org/schema/data/jpa"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
          http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/data/jpa 
          http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd"&gt;
          &lt;!-- Create default configuration for Hibernate --&gt;
          &lt;bean id="hibernateJpaVendorAdapter"
              class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /&gt;
  
          &lt;!-- Configure the entity manager factory bean --&gt;
          &lt;bean id="entityManagerFactory"
              class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
              &lt;property name="dataSource" ref="java:jboss/datasources/PGDB" /&gt;
              &lt;property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" /&gt;
              &lt;!-- Set JPA properties --&gt;
              &lt;property name="jpaProperties"&gt;
                  &lt;props&gt;
                      &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.SQLServerDialect&lt;/prop&gt;
                      &lt;prop key="javax.persistence.schema-generation.database.action"&gt;none&lt;/prop&gt;
                      &lt;prop key="hibernate.ejb.use_class_enhancer"&gt;true&lt;/prop&gt;
                  &lt;/props&gt;
              &lt;/property&gt;
              &lt;!-- Set base package of your entities --&gt;
              &lt;property name="packagesToScan"&gt;
                  &lt;list&gt;
                      &lt;value&gt;com.test.*&lt;/value&gt;
                  &lt;/list&gt;
              &lt;/property&gt;
              &lt;!-- Set share cache mode --&gt;
              &lt;property name="sharedCacheMode" value="ENABLE_SELECTIVE" /&gt;
              &lt;!-- Set validation mode --&gt;
              &lt;property name="validationMode" value="NONE" /&gt;
          &lt;/bean&gt;
  
          &lt;!-- Configure Spring Data JPA and set the base package of the repository 
              interfaces --&gt;
          &lt;jpa:repositories base-package="com.test.*" /&gt;
      &lt;/beans&gt;
  </code></pre>
  
  <p>2) Created beans.xml</p>
  
  <pre><code>            &lt;?xml version="1.0" encoding="UTF-8"?&gt;
              &lt;beans xmlns="http://java.sun.com/xml/ns/javaee"
                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" &gt;
              &lt;/beans&gt;
  </code></pre>
  
  <p>3) Created CdiConfig.java:</p>
  
  <pre><code>            import javax.enterprise.context.Dependent;
              import javax.enterprise.inject.Produces;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
  
              public class CdiConfig {
                  @Produces
                  @Dependent
                  @PersistenceContext
                  public EntityManager entityManager;
              }
  </code></pre>
  
  <p>4) Added to web.xml the following:</p>
  
  <pre><code>    &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/META-INF/applicationContext.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  </code></pre>
  
  <p>5) Implemented a repository:</p>
  
  <pre><code>    import javax.enterprise.inject.Default;
  
      import org.springframework.data.jpa.repository.JpaRepository;
  
      import com.test.MessageBundle;
  
      @Default
      public interface MessageBundleRepository extends JpaRepository&lt;MessageBundle, String&gt; {
  
  
      }
  </code></pre>
  
  <p>6) This is my entity</p>
  
  <pre><code>@Entity
  @Table(name = "WPR_MESSAGE_BUNDLE")
  public class MessageBundle implements BaseEntity {
      private static final long serialVersionUID = 1L;
  
      @Id
      @Column(name = "ID", length = 64)
      private String id;
  
      @Column(name = "MESSAGE", length = 1024)
      private String message;
  
      @Column(name = "LOCALE", length = 16)
      private String locale;
  
      @Column(name = "ACTION", length = 255)
      private String action;
  
      //GETS and SETS
  }
  </code></pre>
  
  <p>7) Stateless Session Bean</p>
  
  <pre><code>@Stateless
  public class MessageBundleService {
      @Inject private MessageBundleRepository repMsgBundle;
  
      public MessageBundleService() {
  
      }
  
      public MessageBundle save(MessageBundle mb) {
          return repMsgBundle.save(mb);
          //return null;
      }
  }
  </code></pre>
  
  <p>I believe CDI part is working properly because MessageBundleRepository is being injected to MessageBundleService but I believe Spring Context is not being loaded on startup because if I add invalid string in applicationContext.xml, nothing happens and I see in log file that default persistence context is being loaded instead of what I configured in applicationContext.xml. Take a look at this log:</p>
  
  <pre><code>    23:59:45,892 INFO  [org.hibernate.dialect.Dialect] (ServerService Thread Pool -- 60) HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
      23:59:45,895 WARN  [org.hibernate.dialect.H2Dialect] (ServerService Thread Pool -- 60) HHH000431: Unable to determine H2 database version, certain features may not work
      23:59:45,958 INFO  [org.hibernate.envers.boot.internal.EnversServiceImpl] (ServerService Thread Pool -- 60) Envers integration enabled? : true
      23:59:46,348 INFO  [org.jboss.weld.Event] (MSC service thread 1-3) WELD-000411: Observer method [BackedAnnotatedMethod] protected org.springframework.data.repository.cdi.CdiRepositoryExtensionSupport.processAnnotatedType(@Observes ProcessAnnotatedType&lt;X&gt;) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
  </code></pre>
  
  <p>Please note my applicationContext.xml configuration is for SQL Server but this logs suggests configuration is being done to H2.</p>
  
  <p>What am I missing? Please keep in mind I don't want to use whole Spring MVC or Spring Boot. Is it possible to use just spring-data-jpa + Hibernate?</p>
  
  <p>The reason I decided to use spring-data-jpa is the feature that allows to scan entities on classpath instead of inform them in configuration file. I need something dynamic because it's a modular application.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>UPDATE (18/06/2017):</strong>
  If I delete beans.xml and CdiConfig.java, Spring seems to work, my entity is found and table is created but I cannot inject MessageBundleRepository (#5) in my session bean MessageBundleService (#7).</p>
  
  <p>Now I suppose there is something wrong on CDI. Probably, EntityManager is being created after JpaRepository framework causing injection failure. Please see following log:</p>
  
  <pre><code>        javax.enterprise.inject.UnsatisfiedResolutionException: Unable to resolve a bean for 'javax.persistence.EntityManager' with qualifiers [@javax.enterprise.inject.Default(), @javax.enterprise.inject.Any()].
              at org.springframework.data.jpa.repository.cdi.JpaRepositoryExtension.createRepositoryBean(JpaRepositoryExtension.java:118)
              at org.springframework.data.jpa.repository.cdi.JpaRepositoryExtension.afterBeanDiscovery(JpaRepositoryExtension.java:94)
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
              at java.lang.reflect.Method.invoke(Method.java:498)
  </code></pre>