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.
<?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.
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.
... 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.
... 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.
... <!-- 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.
... <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> ...