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:


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">

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">

Hibernate.xml file

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <!-- Hibernate session factory -->
        <property name="dataSource">
            <ref bean="dataSource" />
        <property name="hibernateProperties">
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <!-- In prod set to "validate", in test set to "create-drop" -->
                <prop key="">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>
        <property name="packagesToScan" value="net.opensesam" />

Mapping files

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