title: Configuring multiple databases with multiple entitymanagerfactory in spring data jpa
body: <p>I want to configure 2 entity manager factories for 2 different databases(postgres &amp; sql server) in spring 4.</p>
  
  <p>persistence.xml contains 2 persistence units for 2 databases:</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"&gt;
      &lt;persistence-unit name="entityManager"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect"/&gt;
              &lt;property name="hibernate.show_sql" value="false"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt;
              &lt;property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  
      &lt;persistence-unit name="sqlEntityManager"&gt;
          &lt;provider&gt;org.hibernate.jpa.HibernatePersistenceProvider&lt;/provider&gt;
          &lt;properties&gt;
              &lt;property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/&gt;
              &lt;property name="hibernate.show_sql" value="false"/&gt;
              &lt;property name="hibernate.hbm2ddl.auto" value="validate"/&gt;
              &lt;property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>spring-context.xml contains 2 datasources and 2 entitymanager factories :</p>
  
  <pre><code>    &lt;bean id="postgresDataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            p:driverClassName="org.postgresql.Driver"
            p:url="jdbc:postgresql://localhost:5432/test?createDatabaseIfNotExist=true"
            p:username="test"
            p:password="test"/&gt;
  
      &lt;bean id="sqlDataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource"
            p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
            p:url="jdbc:sqlserver://localhost:1433;databaseName=test"
            p:username="test"
            p:password="test"/&gt;
  
  
      &lt;bean id="persistenceUnit" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
          &lt;property name="persistenceXmlLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath*:META-INF/persistence.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="defaultDataSource" ref="postgresDataSource"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sqlPersistenceUnit" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager"&gt;
          &lt;property name="persistenceXmlLocations"&gt;
              &lt;list&gt;
                  &lt;value&gt;classpath*:META-INF/persistence.xml&lt;/value&gt;
              &lt;/list&gt;
          &lt;/property&gt;
          &lt;property name="defaultDataSource" ref="sqlDataSource"/&gt;
      &lt;/bean&gt;
  
  
  
      &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitManager" ref="persistenceUnit"/&gt;
          &lt;property name="persistenceUnitName" value="entityManager"/&gt;
  &lt;property name="packagesToScan" value="com.test.entities.postgres"/&gt;
      &lt;/bean&gt;
  
      &lt;bean id="sqlEntityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitManager" ref="sqlPersistenceUnit"/&gt;
          &lt;property name="persistenceUnitName" value="sqlEntityManager"/&gt;
  &lt;property name="packagesToScan" value="com.test.entities.sql"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>When i run the application it could not create entitymangerfactory as it is checking for tables in postgres which are in sql server.</p>
  
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/spring-context.xml]: Invocation of init method failed; nested exception is org.hibernate.HibernateException: Missing table:&lt;SqlServerTableName&gt;
  </code></pre>