This is part 5 of the Java Technologies Integration tutorial. The purpose of this part is to provide updates to Hibernate configuration in existing project to make the maintenance of the project more straight forward.

Hibernate configuration files

The description of Hibernate configuration was described here: Java Technologies Integration tutorial – part 2 – Hibernate configuration. It is possible to remove hibernate mapping files and use automatic hibernate mapping.

In order to do so the following files should be changed as follows:

Hibernate.xml

sessionFactory bean

Change sessionFactory bean from org.springframework.orm.hibernate3.LocalSessionFactoryBean to org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean. Such change allows using Hibernate annotations automatically.

packagesToScan property

Remove mappingResources property including all the mapping files reference. Instead add packagesToScan property. As the value enter package name, where your classes with hibernate annotations are located, e.g, net.opensesam or to be more specific net.opensesam.entity. Hibernate will scan your package and find all the entities (classes annotated with @Entity) automatically, so you do not have to waste your time on configuration of entity classes.

It is possible to define one package to be scanned:

<property name="packagesToScan" value="net.opensesam" />

as well as multiple locations of Hibernate annotated files:

<property name="packagesToScan">
	<list>
		<value>net.opensesam.entity1</value>
		<value>net.opensesam.entity2</value>
	</list>
</property>

annotatedClasses property

Instead of scanning you can also use annotatedClasses property, but then you have to list all the classes annotated as @Entity explicitly. For example:

<property name="annotatedClasses">
	<list>
		<value>net.opensesam.entity.User</value>
		<value>net.opensesam.entity.Resource</value>
	</list>
</property>

Hibernate.xml file

Modified Hibernate.xml file of the project is presented below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
	<!-- Hibernate session factory -->
	<bean
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
		id="sessionFactory">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${hibernate.dialect}</prop>
				<!-- In prod set to "validate", in test set to "create-drop" -->
				<prop key="hibernate.hbm2ddl.auto">update</prop>
				<!-- In prod set to "false" -->
				<prop key="hibernate.show_sql">false</prop>
				<!-- In prod set to "false" -->
				<prop key="hibernate.format_sql">true</prop>
				<!-- In prod set to "false" -->
				<prop key="hibernate.use_sql_comments">true</prop>
				<!-- In prod set to "false", in test set to "true" -->
				<prop key="hibernate.cache.use_second_level_cache">false</prop>
			</props>
		</property>
		<property name="packagesToScan" value="net.opensesam" />
	</bean>
</beans>

Mapping files

Having the changes introduced you no longer need Hibernate mapping files (*.hbm.xml). Remove them from src/main/resources/resources/hibernate/ directory.