简体   繁体   English

如何为带注释的实体和hbm.xml配置的实体创建休眠会话bean

[英]How to create a hibernate session bean for both Annotated and hbm.xml configured entities

I have legacy hbm.xml configured persistent classes and Annotated persistent classes, so currently we have to specify per which session factory is being used in the DAO bean. 我已经配置了旧的hbm.xml配置的持久化类和带注释的持久化类,因此当前我们必须指定DAO bean中使用的会话工厂。

Unfortunately, that means that I'm running into an issue where we have a mix of DAO's that I'd like to use, aren't all associated to a single session factory. 不幸的是,这意味着我遇到了一个问题,我们混合使用了我想使用的DAO,而这些DAO并没有全部与单个会话工厂相关联。

I'd like to combine both into a single session factory bean since we can't just move everything over all at once. 我想将两者都合并到一个会话工厂bean中,因为我们不能一次移动所有内容。

How would I go about doing this? 我将如何去做呢?

Note: my current workaround is to Annotate some of the xml configured ones and then create two DAO beans: one for each Session Factory. 注意:我当前的解决方法是对一些xml配置的文件进行注释,然后创建两个DAO bean:每个Session Factory一个。

HBM.XML: HBM.XML:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingLocations">
        <value>hbm/path/*.hbm.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

Annotated: 注释:

<bean id="annotatedSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="path.batch.persistent"/>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

Thanks M.Deinum and orid: 感谢M.Deinum和orid:

I was overthinking this. 我想得太多了。
There was never a need for two session factories. 从来不需要两个会话工厂。

So I just had to refactor the context file to: 所以我只需要将上下文文件重构为:

<bean id="annotatedSessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="packagesToScan" value="path.batch.persistent"/>

  <property name="mappingLocations"> <value>hbm/path/*.hbm.xml</value> </property> 

    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
        </props>
    </property>
</bean>

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

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