hibernate entitymanager

I have two database instances, with different table structure, i defined two entitymanagers, two entitymanagerfactories as below
----------------------FIRST SPRING-CONTEXT.XML -----------------------------
<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
            <property name="persistenceXmlLocations">
                  <list>
                        <value>classpath*:META-INF/persistence.xml</value>
                  </list>
            </property>
            <property name="defaultDataSource" ref="dataSource"/>
      </bean>

      <!-- Define the EntityManagerFactory -->
      <bean id="entityManagerFactory"      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
            <property name="persistenceUnitName" value="PU" />
            <property name="dataSource" ref="dataSource"/>            
            <property name="jpaVendorAdapter">
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                        <property name="database" value="MYSQL" />
                        <property name="showSql" value="__MERLIN_JPA_SHOW_SQL__" />
                        <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
                  </bean>
            </property>
            <property name="jpaPropertyMap">
                  <map>
                        <entry key="hibernate.format_sql" value="true" />
                        <entry key="hibernate.hbm2ddl.auto" value="create" />
                  </map>
            </property>
            <property name="jpaDialect">
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
            </property>
      </bean>
      
      <!-- Define an EntityManager -->
      <bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name="entityManagerFactory" ref="entityManagerFactory"/>
      </bean>

----------------------SECOND SPRING-CONTEXT.XML -----------------------------
<bean id="geopersistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
            <property name="persistenceXmlLocations">
                  <list>
                        <value>classpath*:META-INF/geolocate-persistence.xml</value>
                  </list>
            </property>
            <property name="defaultDataSource" ref="geodataSource"/>
      </bean>

      <!-- Define the EntityManagerFactory -->
      <bean id="geoentityManagerFactory"      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="persistenceUnitManager" ref="geopersistenceUnitManager"/>
            <property name="persistenceUnitName" value="GeoPU" />
            <property name="dataSource" ref="geodataSource"/>            
            <property name="jpaVendorAdapter">
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                        <property name="database" value="MYSQL" />
                        <property name="showSql" value="__JPA_SHOW_SQL__" />
                        <property name="databasePlatform" value="merlin.geo.persistence.CustomHSQLDialect" />
                  </bean>
            </property>
            <property name="jpaPropertyMap">
                  <map>
                        <entry key="hibernate.format_sql" value="__JPA_FORMAT_SQL__" />
                        <entry key="hibernate.hbm2ddl.auto" value="create" />
                  </map>
            </property>
            <property name="jpaDialect">
                  <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
            </property>
      </bean>
      
      <!-- Define an EntityManager -->
      <bean id="geoentityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
            <property name="entityManagerFactory" ref="geoentityManagerFactory"/>
      </bean>

aind importing above spring xmls into my main service layer xml like below

----------------------MAIN SPRING-CONTEXT.XML -----------------------------
<import resource="classpath*:business-spring-context.xml"/>
      <import resource="classpath*:geolocate-business-spring-context.xml"/>

when i ma doing this and creating databse through spring, two databases are crated with same tables in both databses with persistence.xmls mixed,
for ex: if firstpersistence.xml has table A,B
and second spring-persistence.xml has table X,Y
resulting databases contains tables A,B,X,Y

please advise a solution

pkseepathiAsked:
Who is Participating?
 
Sathish David Kumar NArchitectCommented:
use ServletContextLoader in web.xml  then both the xml file will load u can use what ever bean id you need the debened upon the request itwill select the xml file 1 or 2
0
 
pkseepathiAuthor Commented:
when i tried to run localling using main method in java and tried to call applicationcontext tables are generated perfectly in two databases, but when i tried to run the same in tomcat, the tables are mixed in both databses, am i doing anything wrong or do i have to set anything on tomcat server please suggest
0
 
pkseepathiAuthor Commented:
not got a desirable solution
0
 
Sathish David Kumar NArchitectCommented:
@pkseepathi:

Thanks for your point ...

But your not come for futher more dicuession then only we dont konw you are getting corecct answer or not ??

Here after if u post a question you also pariticipate in that questiion then only we will know that !!
Thanks ...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.