![](/img/trans.png)
[英]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.