title: Specifying custom persistence unit in spring boot
body: <p>I have the following configuration in my spring boot application:</p>
  
  <pre><code>@Bean(name="entityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory( 
              @Value("${ds.fake.driverClass}") String driverClass,
              @Value("${ds.fake.url}") String url,
              @Value("${ds.fake.user}") String user,
              @Value("${ds.fake.password}") String password,
              @Value("${ds.fake.hibernateDialect}") String hibernateDialect){
  
          LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
  //      bean.setPersistenceUnitName("fake");
          bean.setPersistenceProviderClass(HibernatePersistenceProvider.class);
          bean.setPersistenceUnitManager(persistenceUnitManager(driverClass,url,user,password));
          bean.setJpaVendorAdapter(adapter());
          bean.setJpaProperties((Properties)fakeJpaProperties(hibernateDialect).get());
          bean.setDataSource(fakeDataSource(driverClass,url,user,password));
          //bean.setPackagesToScan("my.required.package");
          bean.afterPropertiesSet();
          return bean;   
      }
      @Bean
      public MergingPersistenceUnitManager persistenceUnitManager(
              @Value("${ds.fake.driverClass}") String driverClass,
              @Value("${ds.fake.url}") String url,
              @Value("${ds.fake.user}") String user,
              @Value("${ds.fake.password}") String password){
          MergingPersistenceUnitManager merger = new MergingPersistenceUnitManager();
  //      merger.setPersistenceXmlLocations("classpath*:META-INF/fake/persistence.xml");
          merger.setPackagesToScan("my.required.package");
  //      merger.setDefaultDataSource(fakeDataSource(driverClass,url,user,password));
          return merger;
      }
  
      @Bean
      public HibernateJpaVendorAdapter adapter(){
          HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
          adapter.setGenerateDdl(false);
          return adapter;
      }
  
      @Bean(initMethod="init",destroyMethod="close")
      public static PoolingDataSource fakeDataSource(
              @Value("${ds.fake.driverClass}") String driverClass,
              @Value("${ds.fake.url}") String url,
              @Value("${ds.fake.user}") String user,
              @Value("${ds.fake.password}") String password) {
          PoolingDataSource bean = new PoolingDataSource();
          bean.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
          bean.setUniqueName("fakeDataSource");
          bean.setMinPoolSize(2);
          bean.setMaxPoolSize(10);
          bean.setMaxIdleTime(900);
          bean.setUseTmJoin(true);
          bean.setDeferConnectionRelease(true);
          bean.setAutomaticEnlistingEnabled(true);
          bean.setAllowLocalTransactions(true);
          bean.setIsolationLevel("READ_COMMITTED");
          Properties props = new Properties();
          props.setProperty("driverClassName", driverClass);
          props.setProperty("url", url);
          props.setProperty("user", user);
          props.setProperty("password", password);
          bean.setDriverProperties(props);
          return bean;
      }
  </code></pre>
  
  <p>If i start up my application I am getting the following error:</p>
  
  <pre><code>Caused by: java.lang.IllegalStateException: Persistence unit with name 'default' already obtained
          at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainPersistenceUnitInfo(DefaultPersistenceUnitManager.java:678)
          at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.determinePersistenceUnitInfo(LocalContainerEntityManagerFactoryBean.java:355)
          at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:307)
          at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
          ... 21 more
  </code></pre>
  
  <p>However, If i Try to uncomment the line <code>bean.setPersistenceUnitName("fake");</code> , I get the new error:</p>
  
  <pre><code>Caused by: java.lang.IllegalArgumentException: No persistence unit with name 'fake' found
          at org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager.obtainPersistenceUnitInfo(DefaultPersistenceUnitManager.java:674)
          at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.determinePersistenceUnitInfo(LocalContainerEntityManagerFactoryBean.java:355)
          at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:307)
          at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
          at com.ixaris.providers.fake.app.boot.FakeAppBooter.entityManagerFactory(FakeAppBooter.java:226)
          at com.ixaris.providers.fake.app.boot.FakeAppBooter$$EnhancerBySpringCGLIB$$6ec884df.CGLIB$entityManagerFactory$2(&lt;generated&gt;)
          at com.ixaris.providers.fake.app.boot.FakeAppBooter$$EnhancerBySpringCGLIB$$6ec884df$$FastClassBySpringCGLIB$$cefb3e29.invoke(&lt;generated&gt;)
          at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
          at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:312)
          at com.ixaris.providers.fake.app.boot.FakeAppBooter$$EnhancerBySpringCGLIB$$6ec884df.entityManagerFactory(&lt;generated&gt;)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:601)
          at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:166)
          ... 24 more
  </code></pre>
  
  <p>How can I go around this / maybe "create" the persistence unit myself?</p>