ScrappyDev
ScrappyDev

Reputation: 2778

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.

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.

I'd like to combine both into a single session factory bean since we can't just move everything over all at once.

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.

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>

Upvotes: 0

Views: 487

Answers (1)

ScrappyDev
ScrappyDev

Reputation: 2778

Thanks M.Deinum and 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>

Upvotes: 1

Related Questions