我目前使用 Spring 3.1 和 Hibernate 4 通过 <context:component-scan>
扫描包中的 DAO 和服务有没有办法对标记为 @Entity
的类(class)做同样的事情?而不是使用 configLocation
属性(property)和 hbm.xml
文件?
<hibernate-configuration>
<session-factory>
<mapping class="com.example.model.User" />
<!-- etc. -->
</session-factory>
</hibernate-configuration>
最佳答案
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="WEB-INF/classes/hibernate.cfg.xml"
p:packagesToScan="com.example.model"
/>
将扫描模型包中的所有内容。我使用我的 cfg.xml 来包含 show_sql 和 hb2ddl.auto 等设置。
关于java - 如何扫描包中的 Hibernate 实体而不是使用 hbm.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11257598/