hibernate multiple persistence.xmls

I have two perssitence.xmls that belongs to two databases
-----------------first persietnce.xml------------
tables --A,B,C

--------second persistence.xml-------------------
tables X,Y,Z

I have main application-context in my service layer where I am importing above business layers xml as below:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:test/merlin-persistence.xml"/>
<!-- <property name="persistenceUnitManager" ref="persistenceUnitManager"/> -->
<property name="persistenceUnitName" value="MerlinPU" />
<property name="dataSource" ref="dataSource"/>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.format_sql" value="true" />
<entry key="hibernate.hbm2ddl.auto" value="update" />
</map>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>

<!-- Define the EntityManagerFactory -->
<bean id="geoentityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<!-- <property name="persistenceUnitManager" ref="geopersistenceUnitManager"/> -->
<property name="persistenceXmlLocation" value="classpath*:test1/geolocate-persistence.xml"/>
<property name="persistenceUnitName" value="GeolocatePU" />
<property name="dataSource" ref="geodataSource"/>
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.format_sql" value="false" />
<entry key="hibernate.hbm2ddl.auto" value="update" />
</map>
</property>
<property name="jpaDialect">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"/>
</property>
</bean>
but when i start my tomcat server ,
two databses have all the tables A,B,C,X,Y,Z ..how can i generate databses only with respeective tables, am i doing anything wrong
pkseepathiAsked:
Who is Participating?
 
Sathish David Kumar NArchitectCommented:
sorry i wrongly mention the name .... rajkumar is correct

contextConfigLocation  only .... instances of ServletContextLoader
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
 
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
 
rajkumar_pbCommented:
Add these lines in web.xml

      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext-hibernate.xml,/WEB-INF/applicationContext-hibernate2.xml</param-value>
      </context-param>
      
      <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

Create two seperate Session Factory for two mapping files.

Call the desired one (one you want to use) in the Services. Thats it
0
 
pkseepathiAuthor Commented:
not a desirable solution
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.