Integrate ZK5 with Spring 3 and Hibernate

From Documentation
DocumentationSmall Talks2011DecemberIntegrate ZK5 with Spring 3 and Hibernate
Integrate ZK5 with Spring 3 and Hibernate

Author
Vincent Jian, Engineer, Potix Corporation
Date
December 12, 2011
Version
ZK5+, Spring 3, Hibernate 3.6

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!

Introduction

Fernando De Leon introduced how to integrate ZK2.1.1, Spring and Hibernate. Since ZK have great improvement from version 2 to version 5, this document will demonstrate how to integrate ZK 5.0.x, Spring 3.0 and Hibernate 3.6 with a sample project that connect to MySQL database step by step.

Step 1: MySQL Database Schema

First we create a database in MySQL called support with two tables, company and contact. Here company and contact has one-to-many relationship. Database schema.jpg

Step 2: Create a Web Application Project

By ZK Studio

  • Create a ZK project
  • If you have installed ZK Studio plugin in Eclispe, refer here to create a simple ZK project.
  • Add Spring 3.0.6 jar files
  • Download jar files from SpringSource Community and copy the following jar files into WEB-INF/lib directory.
    Spring jar.jpg
  • Add Spring 3.0.6 jar files
  • Download jar files from Hibernate and copy the following jar files into WEB-INF/lib directory.
    Hibernate jar.jpg

By Maven

  • Create a Maven Project
  • If you prefer to using maven, refer here to create a ZK project with maven.
  • Modify the pom.xml file to add Spring and Hibernate jar files.
  • a) Add version properties and repositories of Spring and Hibernate.
    <properties>
    	<zk.version>5.0.9</zk.version>
    	<org.springframework.version>3.0.6.RELEASE</org.springframework.version>
    	<hibernate.version>3.6.8.Final</hibernate.version>
    </properties>
    ...
    <repository>
    	<id>repository.springframework.maven.release</id>
    	<name>Spring Framework Maven Release Repository</name>
    	<url>http://maven.springframework.org/release</url>
    </repository>
    <repository>
    	<id>Hibernate repository</id>
    	<url>http://repository.jboss.org/nexus/content/groups/public-jboss/</url>
    </repository>
    
    b) Add dependencies of Spring and Hibernate
    <!-- Spring dependency -->
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-core</artifactId>
    	<version>${org.springframework.version}</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-orm</artifactId>
    	<version>${org.springframework.version}</version>
    </dependency>
    <dependency>
    	<groupId>org.springframework</groupId>
    	<artifactId>spring-web</artifactId>
    	<version>${org.springframework.version}</version>
    </dependency>
    <!-- Hibernate dependency -->
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-core</artifactId>
    	<version>${hibernate.version}</version>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-entitymanager</artifactId>
    	<version>${hibernate.version}</version>
    </dependency>
    <dependency>
    	<groupId>org.hibernate</groupId>
    	<artifactId>hibernate-c3p0</artifactId>
    	<version>${hibernate.version}</version>
    </dependency>
    <!-- MySql dependency start -->
    <dependency>
    	<groupId>mysql</groupId>
    	<artifactId>mysql-connector-java</artifactId>
    	<version>5.1.18</version>
    </dependency>
    

Step 3: Configure Hibernate and Spring

Setup relative configuration files

At the WEB-INF folder of the project we create a Spring configuration file called applicationContext.xml, this file defines the data source (line 9), session factory (line 16) and DAOs (line 29) that are needed to manage Hibernate resources and manage the business objects.

<!-- applicationContext.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/support" />
		<property name="user" value="root" />
		<property name="password" value="root" />
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- set other Hibernate properties in hibernate.cfg.xml file -->
		<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
	</bean>
	
	<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	
	<!-- for using annotation @Transaction in DAOs -->
	<tx:annotation-driven />
	
	<bean id="companyDAO" class="org.zkoss.model.dao.CompanyDAO">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<bean id="companyManager" class="org.zkoss.service.CompanyManagerImpl">
		<property name="companyDAO" ref="companyDAO" />
	</bean>
	<bean id="contactDAO" class="org.zkoss.model.dao.ContactDAO">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>
	<bean id="contactManager" class="org.zkoss.service.ContactManagerImpl">
		<property name="contactDAO" ref="contactDAO" />
	</bean>
</beans>

The configurations above manage Hibernate’s connection to MySQL database by dataSource bean and sessionFactory bean. It also manages the injection of DAOs which are needed to perform different operations on the table (e.g., CRUD operations). And other properties needed in hibernate.cfg.xml are as follow:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		
		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		
		<mapping resource="Company.hbm.xml" />
		<mapping resource="Contact.hbm.xml" />
	</session-factory>
</hibernate-configuration>

Notice that Spring is also able to manage the business layer, review the company manager declaration. The company manager is a business object that uses a company DAO for to define different business rules.

For completeness these are the following Hibernate resources. Create two hibernate bean mapping XML files under classpath folder.

Company.hbm.xml describes the Company bean.

<hibernate-mapping package="org.zkoss.model.bean">
	<class name="Company" table="company">
		<id name="idcompany" column="idcompany" type="integer">
			<generator class="increment" />
		</id>
		<property name="name" column="name" type="string"/>
		<property name="country" column="country" type="string"/>
		<set name="contacts">
			<key column="companyId"/>
			<one-to-many class="org.zkoss.model.bean.Contact"/>
		</set>
	</class>
</hibernate-mapping>

Contact.hbm.xml describes the Contact bean.

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-mapping package="org.zkoss.model.bean">
	<class name="Contact" table="contact">
		<id name="idcontact" column="idcontact" type="integer">
			<generator class="increment" />
		</id>
		<property name="name" column="name" type="string" />
		<property name="email" column="email" type="string" />
		<many-to-one name="company" column="companyId" class="org.zkoss.model.bean.Company" outer-join="true" />
	</class>
</hibernate-mapping>

Create relative Bean and DAO class

Create Company.java to represent the Company bean in memory.

public class Company implements Serializable {
	private Integer idcompany;
	private String name;
	private String country;

	public Company() {}

	// getters and setters
}

And Contact.java to represent the Contact bean in memory.

public class Contact implements Serializable {
	private Integer idcontact;
	private Company company;
	private String name;
	private String email;
	
	public Contact() {}

	// getters and setters
}

The purpose of Spring doesn’t stop just by providing the service for injecting DAOs. In fact Spring provides with a DAO support object (HibernateDaoSupport) which is a base DAO that your DAOs can use. This Spring base DAO enables you to perform the simple CRUD operations in a simple manner.

public class CompanyDAO extends HibernateDaoSupport {

	public void saveOrUpdate(Company company) throws DataAccessException {
		getHibernateTemplate().saveOrUpdate(company);
	}

	public void delete(Company company) throws DataAccessException {
		getHibernateTemplate().delete(company);
	}

	public Company find(Class<Company> clazz, Integer id) throws DataAccessException {
		Company company = (Company) getHibernateTemplate().load(clazz, id);
		return company;
	}
	
	public List<Company> findAll(Class<Company> clazz) throws DataAccessException {
		List<Company> list = getHibernateTemplate().find("from " + clazz.getName());
		return list;
	}
}

Step 4: Implement User Interfaces with ZK

Summary

Comments



Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.