简体   繁体   中英

Using two transaction managers and session factories with spring & hibernate annotations

I have two transactions managers defined in applicationContext, referencing two completely different sessionFactories. I am attempting to use annotations with one of the transactionmanagers however it seems to be applying all annotations to both transaction managers.

The new db I am migrating to has <property name="hbm2ddl.auto">create</property> and the other which I am migrating from has <property name="hbm2ddl.auto">validate</property>

I have annotated my new domain objects with @Entity hibernate is attempting to validate these classes on the legacy database, which I thought I had set up to ignore annotations (by not specifying annotations). Here are the relevant snippets of appContext:

    <tx:annotation-driven transaction-manager="transactionManager1" />
    <context:property-placeholder location="file:${catalina.home}/conf/database.properties" ignore-unresolvable="true"/>

    <bean id="sessionFactory1" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" p:dataSource-ref="dataSource1" p:configLocation="WEB-INF/classes/hibernate.cfg.xml" p:packagesToScan="com.mycompany"/>

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
p:driverClass="${app.driverClassName}" p:jdbcUrl="${app.url}" 
p:user="${app.username}" p:password="${app.password}"

    <!-- Declare a transaction manager-->
    <bean id="transactionManager1" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory1" />

and the legacy db in same appContext :

<bean id="sessionFactory2" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"    p:dataSource-ref="dataSource2" p:configLocation="WEB-INF/classes/hibernateTraveller.cfg.xml" p:packagesToScan="com.mycompany"/>

<!-- Declare a datasource that has pooling capabilities-->   
<bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" 
p:driverClass="${app2.driverClassName}" p:jdbcUrl="${app2.url}" 
p:user="${app2.username}" p:password="${app2.password}"

<!-- Declare a transaction manager-->
<bean id="transactionManager2" class="org.springframework.orm.hibernate3.HibernateTransactionManager" 
            p:sessionFactory-ref="sessionFactoryTraveller"> 
<qualifier value="legacyDB"/>
</bean>

any pointers ?

You should use LocalSessionFactoryBean instead of AnnotationSessionFactoryBean for your legacy database that does not use annotations. The whole purpose of the AnnotationSessionFactoryBean is to use annotations. If you don't want to use annotations, LocalSessionFactoryBean is the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM