How do you configure the Hibernate class mappings of class org.springframework.orm.hibernate3.LocalSessionFactoryBean in the Spring application context? I want to move the session factory class mappings from the following hibernate.cfg.xml
to the corresponding Spring session factory bean so that I may eliminate hibernate.cfg.xml
.
File 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>
File 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>
The ApplicationContext InterfaceIt uses dependency injection to achieve inversion of control. The interfaces BeanFactory and ApplicationContext represent the Spring IoC container. Here, BeanFactory is the root interface for accessing the Spring container. It provides basic functionalities for managing beans.
We can have multiple application contexts that share a parent-child relationship. A context hierarchy allows multiple child contexts to share beans which reside in the parent context. Each child context can override configuration inherited from the parent context.
Spring Boot injects the application context into the parameter of the setApplicationContext() method, where we get the Id of the Spring application. (The Id here is the name of the application.) In the Application , we create a bean, call its method and set up the Spring Boot application.
If you're using JPA-annotated classes, you can use AnnotationSessionFactoryBean
instead of LocalSessionFactoryBean
, and inject the classes directly into the 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>
As a slight variation on skaffman's answer, I used property packagesToScan
of class AnnotationSessionFactoryBean
to avoid listing all of the individual model class names:
<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>
I learned about this property from the excellent book Spring in Action, Third Edition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With