Friday, December 18, 2009

Spring, JSF & Hibernate Integration - Part III

This tutorial mentions Step-by-Step instructions for developing Spring, JSF & Hibernate integrated application. It totally has three parts:

I. Creating simple Spring application
II. Creating application involving Spring and JSF
III. Creating application involving Spring, JSF and Hibernate

The current tutorial deals with the third part of creating application involving Spring, JSF & Hibernate.

The final netbeans project after completing Part III of the current tutorial has been uploaded here. Because of size limitations the third party jars inside the lib folder has been deleted in the uploaded zip file.

Requirements :: Netbeans 6.7.1 with Java 6 installed

Ensure that you have gone through the first and second part of tutorial.

You can continue the below steps starting with the netbeans project available here which is obtained after performing steps in Part II of the tutorial

27. Create links database table and insert some sample records into it. The DDL for the links table is given below: -


After table creation, insert some records into it.

28. Create linkedlinks.hbm.xml file determining the mapping between Link POJO domain object and database table LINKS. Create a folder with name resources under the java sources root folder and place the xml file inside resources folder

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.linkedlinks.domain.Link" table="LINKS">
<id name="linkId" column="LINK_ID">
<generator class="increment"/>
<property name="httpLink">
<column name="HTTP_LINK"/>
<property name="title">
<column name="TITLE"/>
<property name="description">
<column name="DESCRIPTION"/>

The generator class for linkId attribute is set as increment. This takes care of automatically populating the link id property of newly created link to next available ID.

29. Create a class implementing LinkDao which uses hibernate to persist the data. Name it as under the repository package.

class java

30. Create a property file under WEB-INF folder containing details of jdbc database settings.


Ensure that you have included the jar containing the above driver class in your project libraries.

31. Create bean definitions with ids propertyConfigurer and myDataSource in applicationContext.xml as given below.

<bean id="propertyConfigurer"
p:location="/WEB-INF/" />
<bean id="myDataSource"
p:password="${jdbc.password}" />

32. Define session factory and hibernate template beans in applicationContext.xml

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<property name="hibernateProperties">
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>

33. Create a new bean definition on HibernateLinkDao class in applicationContext.xml file. Also set the previously defined hibernateTemplate bean as the value of hibernateTemplate property of this bean. Change the linkDao property reference bean value of myService bean from linkDao to myHibenrateLinkDao. Now the service uses the hibernate dao class instead of SimulateDao class.

<bean id="myHibernateLinkDao" class="com.linkedlinks.repository.HibernateLinkDao">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
<bean id="myService" class="com.linkedlinks.service.LinksServiceImpl">
<property name="linkDao">
<ref bean="myHibernateLinkDao" />

34. Run createlink.jsp and pages will be displayed accordingly. The data will be retreived from database tables and any new link creations will be persisted in the DB.

This completes the tutorial of integrating Spring, JSF & Hibernate technologies.

No comments:

Post a Comment