[英]How to scan packages for Hibernate entities instead of using hbm.xml?
[英]How to create a hibernate session bean for both Annotated and hbm.xml configured entities
我已經配置了舊的hbm.xml配置的持久化類和帶注釋的持久化類,因此當前我們必須指定DAO bean中使用的會話工廠。
不幸的是,這意味着我遇到了一個問題,我們混合使用了我想使用的DAO,而這些DAO並沒有全部與單個會話工廠相關聯。
我想將兩者都合並到一個會話工廠bean中,因為我們不能一次移動所有內容。
我將如何去做呢?
注意:我當前的解決方法是對一些xml配置的文件進行注釋,然后創建兩個DAO bean:每個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>
注釋:
<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>
感謝M.Deinum和orid:
我想得太多了。
從來不需要兩個會話工廠。
所以我只需要將上下文文件重構為:
<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.