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

Steps
=====
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: -

CREATE TABLE LINKS (
LINK_ID INT NOT NULL,
TITLE VARCHAR(150),
HTTP_LINK VARCHAR(100),
DESCRIPTION VARCHAR(3000)
);

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"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.linkedlinks.domain.Link" table="LINKS">
<id name="linkId" column="LINK_ID">
<generator class="increment"/>
</id>
<property name="httpLink">
<column name="HTTP_LINK"/>
</property>
<property name="title">
<column name="TITLE"/>
</property>
<property name="description">
<column name="DESCRIPTION"/>
</property>
</class>
</hibernate-mapping>

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 HibernateLinkDao.java under the repository package.

class java


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

jdbc.driverClassName=JDBC_DRIVER_QUALIFIED_CLASS_NAME
jdbc.url=JDBC_CONNECTION_URL
jdbc.username=USERNAME
jdbc.password=YOUR_PASSWORD

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"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
p:location="/WEB-INF/links.properties" />
<bean id="myDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="${jdbc.driverClassName}"
p:url="${jdbc.url}"
p:username="${jdbc.username}"
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">
<list>
<value>./resources/linkedlinks.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="mySessionFactory"/>
</property>
</bean>


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" />
</property>
</bean>
<bean id="myService" class="com.linkedlinks.service.LinksServiceImpl">
<property name="linkDao">
<ref bean="myHibernateLinkDao" />
</property>
</bean>

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