Hibernate Integration

How to manage mapped classes

To list, load, save and update entities Riot provides a DAO interface. When your application uses Hibernate to access its data, you don't have to implement the interface yourself, instead you can use the classes provided by the riot-hibernate module.

Top-level lists

In order to display a list of all instances of a mapped class you can use the HqlDao and simply specify the class name. Just add the following list configuration to /WEB-INF/riot-config/lists.xml:

<list id="vets">
    <dao class="org.riotfamily.riot.hibernate.dao.HqlDao">
        <property name="sessionFactory" ref="sessionFactory" />
        <property name="entityClass" value="org.springframework.samples.petclinic.Vet" />
    </dao>
    <columns>
        <column property="firstName" />
        <column property="lastName" />
    </columns>
</list>

The HqlDao supports paging, searching, fitering, ordering and item swapping.

Child lists

Another commonly used DAO implementation is the HqlParentChildDao which allows you to define nested lists for entities with a parent-child relationship. The petclinc example uses this DAO for the list of an owner's pets:

<dao class="org.riotfamily.riot.hibernate.dao.HqlParentChildDao">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="entityClass" value="org.springframework.samples.petclinic.Pet" />
    <property name="parentProperty" value="owner" />
</dao>

The HqlParentChildDao supports cut-and-paste operations and therefore allows you to move a child from one parent to another.

Working with collections

If the child objects are mapped as collection with inverse=”false” you can use the HqlCollectionDao which loads the parent object and works on the specified collection. In contrast to HqlDao and HqlParentChildDao the HqlCollectionDao does not support searching, item swapping and querying by example.

If you want to be able to swap items, i.e. use the move-up and move-down commands, you have to use a HqlSortedCollectionDao or HqlIndexedCollectionDao.

To save you some typing, the riot-hibernate module defines some prototype beans that can be used as shortcut:

<dao ref="hqlDao">
    <property name="entityClass" value="org.springframework.samples.petclinic.Vet" />
</dao>

The names of the prototypes are hqlDao, hqlParentChildDao, hqlCollectionDao, hqlSortedCollectionDao and hqlINdexedCollectionDao.