Archived Content

The UP3 space contains documentation related to the next-generation portal exploration work that JA-SIG has now archived. Documentation related to uPortal, including active development for future releases, can be found in the uPortal space.
References to uPortal 3 in this space should be considered out of date. Active uPortal 3 documentation is available in the uPortal space.

Hibernate Setup and Configuration

This page contains information on how hibernate is setup for the DAOs that use hibernate.

Setup

Setup is only necessary if you need to create new POJO classes and/or mapping files. If you are not using Eclipse you can use Middlegen. There is a very easy to use eclipse plugin Hibernate Tool for reverse engineering of the POJO classes and mapping files from an existing database. On the Hibernate Tools page, there is a flash tutorial on how to use the tool. The files that are auto-generated will need a litte touch up.

Sample Mapping File

The mapping file tells hibernate which POJO class maps to what table and what field map to which column in that table. It is recommended that all queries are placed in these mapping files. Hibernate uses HQL to retrieve data.

PortletWindowItem.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class
        name="org.jasig.portal.portlet.dao.hibernate.item.PortletWindowItem"
        table="UP_PORT_WIN">
       
        <id name="id" type="integer">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        ...
    </class>
    <query name="FIND_PORT_WIN_BY_ID">
        <![CDATA[
            select pwe from PortletWindowItem pwe where pwe.id = :pId
        ]]>
    </query>
    ...
</hibernate-mapping>

Sample POJO Class

POJO classes basically contain fields that have setter and getter methods. Hibernate uses them for persistence and as return objects on queries.

PortletWindowItem.java
public class PortletWindowItem  implements java.io.Serializable {
    // Fields    
    private Integer id;
    ...
    // Property accessors
    /**
     * Portlet window id
     */
    public Integer getId() {
        return this.id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
   ...
}

Sample Query Call

The query is declared in the mapping file.

HibernatePortletWindowDao.java
...
    Session session = getSession();
    try {
        Query findPortletWindowById = session.getNamedQuery("FIND_PORT_WIN_BY_ID");
        int winId = portletWindowId.toInt();
        findPortletWindowById.setInteger("pId", winId);
        List windows = findPortletWindowById.list();
        ...
    }
    catch (HibernateException he) {
        throw convertHibernateAccessException(he);
    }
    releaseSession(session);
...

Configuration

Jars Needed By Hibernate 3.0.5

commons-logging.jar
commons-collections-3.1.jar
antlr-2.7.5H3.jar
asm.jar
asm-attrs.jar
cglib-2.1jar
dom4j-1.6.jar
hibernate3.jar
jta.jar

Rdbm Properties

Add hibernate database dialect to database properties file.

rdbm.properties
...
    hibernateDialect=org.hibernate.dialect.HSQLDialect
...

Build XML

Add jars to the build path and copy the hibernate mapping files on the compile target in the build.xml.

build.xml
...
    <!-- Copy the hibernate mapping files -->
    <copy todir="${build.home}/WEB-INF/classes/hibernate-mappings">
        <fileset dir="hibernate-mapping/org/jasig/portal/portlet/dao/hibernate/item" includes="**/*.hbm.xml" />
    </copy>
...

Persistence Beans XML

Add session factory and DAO beans to the persistence_beans.xml. The data source does not need to be changed.

persistence_beans.xml
...
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource"><ref local="dataSource" /></property>
        <property name="mappingResources">
        <list>
            <value>hibernate-mappings/PortletWindowItem.hbm.xml</value>
            ...
        </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernateDialect}</prop>
            </props>
        </property>
    </bean>
    ...
    <bean id="portletWindowDao" 
        class="org.jasig.portal.portlet.dao.hibernate.HibernatePortletWindowDao" 
        lazy-init="true">

        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
        <property name="incrementer"><ref bean="portletWindowIncrementer"/></property>
    </bean>
    ...
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 14, 2005

    Dave Brondsema says:

    Note that to lazyload collections (the default in Hibernate 3, IIUC!) you must u...

    Note that to lazy-load collections (the default in Hibernate 3+, IIUC!) you must use something like the OpenSessionInView pattern (http://hibernate.org/43.html). This is often done for servlets with Spring's OpenSessionInViewFilter or Spring WebMVC's OpenSessionInViewInterceptor. In portlets, however, there are 2 requests (action and render) and the OpenSessionInViewFilter only covers one request lifespan. The only solution I know of is a Spring PortletMVC OpenSessionInViewInterceptor (discussed at http://forum.springframework.org/viewtopic.php?t=4907 with details and downloads at http://opensource2.atlassian.com/confluence/spring/display/JSR168/Home) but it requires you to use a Spring poortlet and Portlet MVC framework.