Der*_*har 9 java configuration spring hibernate
如何在Spring 应用程序上下文中配置类org.springframework.orm.hibernate3.LocalSessionFactoryBean的Hibernate类映射?我想将会话工厂类映射从以下内容移动到相应的Spring会话工厂bean,以便我可以消除. hibernate.cfg.xmlhibernate.cfg.xml
档案hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- ... -->
<mapping resource="Queries.hbm.xml" />
<mapping class="com.company.app.common.model.Account" />
<mapping class="com.company.app.common.model.AccountCategory" />
<mapping class="com.company.app.common.model.AssetType" />
<mapping class="com.company.app.common.model.Book" />
<mapping class="com.company.app.model.AssetTypeCategory" />
<!-- ... -->
</session-factory>
</hibernate-configuration>
Run Code Online (Sandbox Code Playgroud)
档案spring-application-context.xml:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configurationClass">
<value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<!-- Instead of the above, I want to use the following. Where and
how do I define the class mappings so that I may eliminate
hibernate.cfg.xml? -->
<--
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>Queries.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
-->
</bean>
Run Code Online (Sandbox Code Playgroud)
ska*_*man 25
如果您正在使用JPA注释类,则可以使用AnnotationSessionFactoryBean而不是LocalSessionFactoryBean直接将类注入到Spring bean中:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.company.app.common.model.Account</value>
<value>com.company.app.common.model.AccountCategory</value>
<value>com.company.app.common.model.AssetType</value>
<value>com.company.app.common.model.Book</value>
<value>com.company.app.model.AssetTypeCategory</value>
</list>
</property>
<property name="mappingResources">
<list>
<value>Queries.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
作为skaffman 答案的一个细微变化,我使用packagesToScan了类的属性AnnotationSessionFactoryBean来避免列出所有单独的模型类名:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.company.app.common.model</value>
</list>
</property>
<property name="mappingResources">
<list>
<value>Queries.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
</props>
</property>
</bean>
Run Code Online (Sandbox Code Playgroud)
我从优秀的Spring in Action,第三版中了解到这个属性.
| 归档时间: |
|
| 查看次数: |
21732 次 |
| 最近记录: |