title: Spring JPA Config IllegalArgumentException: No persistence unit with name found
body: <p>I am having a weird issue I'm not able to wrap my head around. I have used JPA/Hibernate is Spring before with NO persistence.xml file, Spring handled everything. I'm working on a new project and this time I decided to go all Java Config. I am having some issues with my PersistenceConfig.java, it keeps saying it can't find a persistence unit. If I comment out the line the sets the PersistenceUnitName then it complains IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}.</p>
  
  <p>I don't understand why its trying to use a persistence.xml when I use Java Config and not when I use XML. Any solutions? </p>
  
  <p>Here is my PersistenceConfig.java</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.core.env.Environment;
  import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
  import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
  import org.springframework.orm.jpa.JpaDialect;
  import org.springframework.orm.jpa.JpaTransactionManager;
  import org.springframework.orm.jpa.JpaVendorAdapter;
  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
  import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
  import org.springframework.transaction.PlatformTransactionManager;
  import org.springframework.transaction.annotation.EnableTransactionManagement;
  
  import javax.persistence.EntityManagerFactory;
  import javax.sql.DataSource;
  import java.util.Properties;
  
  @Configuration
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = {"com.testapp.ots.repository"})
  public class PersistenceConfig {
  
      @Autowired
      Environment environment;
  
      @Bean(name = "datasource")
      public DataSource dataSource() {
          JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
          dsLookup.setResourceRef(true);
          return dsLookup.getDataSource("jdbc/postgres");
      }
  
      @Bean(name = "entityManagerFactory")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
          entityManagerFactory.setDataSource(dataSource());
          entityManagerFactory.setPersistenceUnitName("postgres");
          entityManagerFactory.setJpaProperties(jpaProperties());
          entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter());
          entityManagerFactory.setJpaDialect(jpaDialect());
          return entityManagerFactory;
      }
  
      @Bean(name = "jpaVendorAdapter")
      public JpaVendorAdapter jpaVendorAdapter() {
          HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQL9Dialect");
          vendorAdapter.setGenerateDdl(true);
          vendorAdapter.setShowSql(true);
          return vendorAdapter;
      }
  
      @Bean(name = "jpaDialect")
      public JpaDialect jpaDialect() {
          return new HibernateJpaDialect();
      }
  
      @Bean(name = "transactionManager")
      public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
          JpaTransactionManager jpaTransactionManager = new JpaTransactionManager(entityManagerFactory);
          jpaTransactionManager.setDataSource(dataSource());
          jpaTransactionManager.setJpaDialect(jpaDialect());
          return jpaTransactionManager;
      }
  
      @Bean(name = "persistenceExceptionTranslation")
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      public Properties jpaProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", environment.getProperty("hibernate.hbm2ddl.auto"));
          properties.setProperty("hibernate.dialect", environment.getProperty("hibernate.dialect"));
          properties.setProperty("hibernate.show_sql", environment.getProperty("hibernate.show_sql"));
          properties.setProperty("hibernate.format_sql", environment.getProperty("hibernate.format_sql"));
          properties.setProperty("hibernate.connection.charSet", environment.getProperty("hibernate.connection.charSet"));
          properties.setProperty("hibernate.cache.use_second_level_cache", environment.getProperty("hibernate.cache.use_second_level_cache"));
          properties.setProperty("hibernate.cache.use_query_cache", environment.getProperty("hibernate.cache.use_query_cache"));
          properties.setProperty("hibernate.cache.use_structured_entries", environment.getProperty("hibernate.cache.use_structured_entries"));
          properties.setProperty("hibernate.generate_statistics", environment.getProperty("hibernate.generate_statistics"));
          return properties;
      }
  }
  </code></pre>
  
  <p>As a reference here is the XML configuration I used a few months back.</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:context="http://www.springframework.org/schema/context"
         xmlns:jee="http://www.springframework.org/schema/jee"
         xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
         xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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.xsd"&gt;
  
      &lt;context:annotation-config /&gt;
  
      &lt;jpa:repositories base-package="net.jkratz.bloodpressure.api.repository" /&gt;
  
      &lt;jee:jndi-lookup jndi-name="jdbc/BloodPressureDB" id="dataSource" expected-type="javax.sql.DataSource" /&gt;
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="BPPersistenceUnit" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaVendorAdapter" ref="jpaVendorAdapter" /&gt;
          &lt;property name="jpaDialect" ref="jpaDialect" /&gt;
          &lt;property name="packagesToScan"&gt;
              &lt;list&gt;
                  &lt;value&gt;net.jkratz.bloodpressure.api.model&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="jpaProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;
                  &lt;prop key="hibernate.hbm2ddl.auto"&gt;${hibernate.hbm2ddl.auto}&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.format_sql"&gt;${hibernate.format_sql}&lt;/prop&gt;
                  &lt;prop key="hibernate.connection.charSet"&gt;${hibernate.connection.charSet}&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
          &lt;property name="database" value="${jpa.vendor.database}" /&gt;
          &lt;property name="showSql" value="${jpa.vendor.showSql}"/&gt;
          &lt;property name="generateDdl" value="${jpa.vendor.generateDdl}"/&gt;
          &lt;property name="databasePlatform" value="${jpa.vendor.databasePlatform}"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
          &lt;property name="jpaDialect" ref="jpaDialect" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  
  &lt;/beans&gt;
  </code></pre>