简体   繁体   English

使用两个带有Spring和Hibernate注释的事务管理器和会话工厂

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

I have two transactions managers defined in applicationContext, referencing two completely different sessionFactories. 我在applicationContext中定义了两个事务管理器,它们引用了两个完全不同的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> 我要迁移到的新数据库具有<property name="hbm2ddl.auto">create</property> ,而我要迁移的另一个数据库具有<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). 我已经用@Entity注释了我的新域对象,hibernate试图在旧数据库上验证这些类,我以为我已经设置为忽略注释(通过不指定注释)。 Here are the relevant snippets of appContext: 以下是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 : 和旧数据库在同一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. 对于不使用注释的旧数据库,应使用LocalSessionFactoryBean而不是AnnotationSessionFactoryBean The whole purpose of the AnnotationSessionFactoryBean is to use annotations. AnnotationSessionFactoryBean的全部目的是使用注释。 If you don't want to use annotations, LocalSessionFactoryBean is the way to go. 如果您不想使用注释,则可以使用LocalSessionFactoryBean。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有Spring和Hibernate的会话工厂处理多个数据库连接 - handling multiple database connections using session factories with Spring and Hibernate 跨越两个 Hibernate 事务管理器的一种服务方法上的 Spring @Transactional - Spring @Transactional on one service method spanning over two Hibernate transaction managers 带注释的Spring + Hibernate:没有Hibernate会话绑定到线程 - Spring + Hibernate with annotations: No Hibernate Session bound to thread 在Spring中使用JPA EntityManager和Hibernate会话与共享事务管理器 - Using both JPA EntityManager and Hibernate session with shared transaction manager in Spring Spring 3 与 Hibernate 3 注释 - Session 出厂配置 - Spring 3 with Hibernate 3 Annotations - Session Factory Configuration 使用多个转换管理器时,没有Hibernate会话绑定到该线程 - No Hibernate session bound to the thread while using multiple transation managers 在 Spring 集成中使用多个事务管理器时出现 NoUniqueBeanDefinitionException - NoUniqueBeanDefinitionException when using multiple transaction managers in Spring Integration Spring / Hibernate / HSQLDB组合的会话/事务问题 - Session / Transaction problems with Spring/Hibernate/HSQLDB combo 由春季交易经理管理的“暂停”休眠会话 - “suspend” hibernate session managed by spring transaction manager Spring/Hibernate/TestNG - 手动 session 和事务设置 - Spring/Hibernate/TestNG - manual session and transaction setup
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM