title: Use EntityManager in Springboot
body: <p>I want to use <code>EntityManager</code> in <code>SpringBoot</code>.</p>
  
  <p><strong>Application</strong></p>
  
  <pre><code>@Configuration
  @EnableRetry // To enable Spring retry
  @EnableJpaRepositories
  @EnableAspectJAutoProxy(proxyTargetClass=true)
  @SpringBootApplication
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
      @Bean
      public RestTemplate restTemplate() {
          return new RestTemplate();
      }    
  }
  </code></pre>
  
  <p><strong>MailConfig</strong></p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(
          entityManagerFactoryRef = "mailEntityManager",
          transactionManagerRef = "mailTransactionManager",
          basePackageClasses = MmcMonitoringLog.class)
  
  public class MailConfig {
  
      @Autowired(required = false)
      private PersistenceUnitManager persistenceUnitManager;
  
      @Bean
      @ConfigurationProperties("app.order.jpa")
      public JpaProperties orderJpaProperties() {
          return new JpaProperties();
      }
  
      @Bean
      @ConfigurationProperties(prefix = "app.order.datasource")
      public DataSource orderDataSource() {
          return (DataSource) DataSourceBuilder.create().type(DataSource.class).build();
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean orderEntityManager(
              JpaProperties orderJpaProperties) {
          EntityManagerFactoryBuilder builder = createEntityManagerFactoryBuilder(orderJpaProperties);
          return builder
                  .dataSource(orderDataSource())
                  .packages(MmcMonitoringLog.class)
                  .persistenceUnit("ordersDs")
                  .build();
      }
  
      @Bean
      public JpaTransactionManager orderTransactionManager(EntityManagerFactory orderEntityManager) {
          return new JpaTransactionManager(orderEntityManager);
      }
  
      private EntityManagerFactoryBuilder createEntityManagerFactoryBuilder(JpaProperties customerJpaProperties) {
          JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(customerJpaProperties);
          return new EntityManagerFactoryBuilder(jpaVendorAdapter,
                  customerJpaProperties.getProperties(), this.persistenceUnitManager);
      }
  
      private JpaVendorAdapter createJpaVendorAdapter(JpaProperties jpaProperties) {
          AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
          adapter.setShowSql(jpaProperties.isShowSql());
          adapter.setDatabase(jpaProperties.getDatabase());
          adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform());
          adapter.setGenerateDdl(jpaProperties.isGenerateDdl());
          return adapter;
      }
  
  }
  </code></pre>
  
  <p><strong>MailService</strong></p>
  
  <pre><code>public class MailService extends TaskAdaptor implements Runnable {
  
      @Autowired
      MmcMonitoringLogRepository mmcMonitoringLogRepository;
  
      @Override
      public void run() {
  
     List&lt;MmcMonitoringLog&gt; list = mmcMonitoringLogRepository.findByMonitoringLog("1");
     ......
  }
  </code></pre>
  
  <p><strong>appication.properties</strong></p>
  
  <pre><code>spring.datasource.url= jdbc:mysql://xxxx:3306/adb?autoReconnect=true&amp;useSSL=false
  spring.datasource.username=xxx
  spring.datasource.password=xxx
  
  
  # The SQL dialect makes Hibernate generate better SQL for the chosen database
  spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
  
  # Hibernate ddl auto (create, create-drop, update)
  spring.jpa.hibernate.ddl-auto = none
  </code></pre>
  
  <p>I try to implement following <a href="https://github.com/snicoll-demos/demo-multi-entity-managers/blob/master/src/main/java/demo/order/OrderConfig.java" rel="nofollow noreferrer">https://github.com/snicoll-demos/demo-multi-entity-managers/blob/master/src/main/java/demo/order/OrderConfig.java</a>, but my SpringBoot stop straight away without throwing any exception. If I remove MailConfig, Springboot can start. What's the problem ? Am I on the right path ?</p>