Friday, 1 February 2013

MVVM CRUD with Spring 3.1 and Hibernate 4 and ZK - Part 1

This is the first of a series of articles about Persistence with Spring and ZK as presentation layer.


Technologies Used

    1. ZK 6.5.1 CE Version
    3. Hibernate Core Version 4.1.9 Final
    4. Spring ORM Version 3.2.0
    5. MySQL version 5
Step 1:
If you are new ZK, then you can setup the Development environment by downloading this document.
Step 2:
MySQL Setup. Let us create the following table in MySQL. Let us create the following table and insert two records by default.
CREATE TABLE `addressbook` (
`ID` bigint(20) NOT NULL auto_increment,
`FullName` varchar(255) default NULL,
`Address1` varchar(255) default NULL,
`Address2` varchar(255) default NULL,
`City` varchar(255) default NULL,
`State` varchar(255) default NULL,
`ZipCode` varchar(255) default NULL,
`Email` varchar(255) default NULL,
`Mobile` varchar(255) default NULL,

Step 3:
Now let us create ZK Maven Project. By default POM File will point ZK 6.0.1 Version. we will change this to latest version 6.5.1 and also we will remove Commercial version of ZK from the POM File. we will include all the other dependency. You can copy the below POM.xml

<project xmlns="" xmlns:xsi=""
<name>The AddressBook Project</name>
<description>The AddressBook Project</description>
<id>ZK CE</id>
<name>ZK CE Repository</name>
<name>ZK Maven Plugin Repository</name>


<!-- Spring framework -->

<dependency> <!-- Used for Hibernate4 LocalSessionFactoryBean -->

<!-- AOP dependency -->

<!-- Persistence Management -->
<dependency> <!-- Apache BasicDataSource -->

<dependency> <!-- MySQL database driver -->


<!-- ZK 5 breeze theme <dependency> <groupId>org.zkoss.theme</groupId>
<artifactId>breeze</artifactId> <version>${zk.version}</version> <optional>true</optional>
</dependency> -->
<!-- Run with Jetty -->
<!-- Compile java -->
<!-- Build war -->
<!-- Pack zips -->

Step 4:
Our Next step to update the web.xml with spring ContextListner and load our own configuration file as follows

<!-- Loads the Spring web application context -->

Step 5:
Now let us create the applicationContext.xml file under WEB-INF Folder as follows
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:p="" xmlns:xsi=""
xmlns:context="" xmlns:tx=""

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/addressbook" />
<property name="username" value="root" />
<property name="password" value="1234" />

<bean id="sessionFactory"
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="AddressBook.domain" />
<property name="hibernateProperties">
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="">update</prop>
<prop key="hibernate.show_sql">true</prop>

<bean id="transactionManager"
<property name="sessionFactory" ref="sessionFactory" />

<context:annotation-config />
<context:component-scan base-package="AddressBook" />

<tx:annotation-driven transaction-manager="transactionManager" />

<!-- This will ensure that hibernate or jpa exceptions are automatically
translated into Spring's generic DataAccessException hierarchy for those
classes annotated with Repository -->

class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="CRUDService" class=" AddressBook.service.CRUDServiceImpl" />


Step 6:
Now let us create the domain object for addressbook in the package AddressBook.domain

package AddressBook.domain;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

public class AddressBook implements Serializable {

private static final long serialVersionUID = 1L;

private long id;

private String fullName;
private String address1;
private String address2;
private String city;
private String zipCode;
private String state;
private String email;
private String mobile;

public String getFullName() {
return fullName;

public void setFullName(String fullName) {
this.fullName = fullName;

public String getAddress1() {
return address1;

public void setAddress1(String address1) {
this.address1 = address1;

public String getAddress2() {
return address2;

public void setAddress2(String address2) {
this.address2 = address2;

public String getCity() {
return city;

public void setCity(String city) { = city;

public String getZipCode() {
return zipCode;

public void setZipCode(String zipCode) {
this.zipCode = zipCode;

public String getState() {
return state;

public void setState(String state) {
this.state = state;

public String getEmail() {
return email;

public void setEmail(String email) { = email;

public String getMobile() {
return mobile;

public void setMobile(String mobile) { = mobile;

public long getId() {
return id;

public void setId(long id) { = id;


Step 7:
Now let us create server and DAO Layer as follows

package AddressBook.dao;

import java.util.List;

public interface CRUDDao {
<T> List<T> getAll(Class<T> klass);

package AddressBook.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

public class CRUDDaoImpl implements CRUDDao {

SessionFactory sessionFactory;

public <T> List<T> getAll(Class<T> klass) {
return getCurrentSession().createQuery("from " + klass.getName())

protected final Session getCurrentSession() {
return sessionFactory.getCurrentSession();


package AddressBook.service;

import java.util.List;

public interface CRUDService {
<T> List<T> getAll(Class<T> klass);

package AddressBook.service;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import AddressBook.dao.CRUDDao;

public class CRUDServiceImpl implements CRUDService {

private CRUDDao CRUDDao;

@Transactional(readOnly = true)
public <T> List<T> getAll(Class<T> klass) {
return CRUDDao.getAll(klass);


Step 8:
Now let us create listing screen using ZK as follows. Create AddressList.zul in the folder Pages(create this folder) under webapp as shown

<?page title="Attorney List" contentType="text/html;charset=UTF-8"?>
<window id="addressList" title="Address List" border="none"
viewModel="@id('vm') @init('zkoss.vm.AddressListVM')">
<separator />
<listbox id="" mold="paging" pageSize="11" pagingPosition="top"
selectedItem="@bind(vm.selectedItem)" model="@load(vm.dataSet)">
<listhead sizable="true">
<listheader label="Full Name" sortDirection="ascending"
sort="auto(fullName)" />
<listheader label="Email" sort="auto(email)" />
<listheader label="Mobile" sort="auto(mobile)" />
<template name="model" var="p1">
<listcell label="@load(p1.fullName)" />
<listcell label="@load(" />
<listcell label="@load(" />

Step 9:
Now let us create the View model for the above ZUL File.

package zkoss.vm;

import java.util.List;

import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.zk.ui.Component;
import org.zkoss.zkplus.spring.SpringUtil;

import AddressBook.domain.AddressBook;
import AddressBook.service.CRUDService;

public class AddressListVM {

private CRUDService CRUDService;

private AddressBook selectedItem;
private List<AddressBook> allReordsInDB = null;

public AddressBook getSelectedItem() {
return selectedItem;

public void setSelectedItem(AddressBook selectedItem) {
this.selectedItem = selectedItem;

public void initSetup(@ContextParam(ContextType.VIEW) Component view) {
Selectors.wireComponents(view, this, false);
CRUDService = (CRUDService) SpringUtil.getBean("CRUDService");
allReordsInDB = CRUDService.getAll(AddressBook.class);

public List<AddressBook> getDataSet() {
return allReordsInDB;


Step 10
Now right click on AddressList.zul and Select Run as “Run on Server” to see the following output.


Project Structure


You can download the source here

Spring with Hibernate persistence and transactions example
2. The Persistence Layer with Spring 3.2 and Hibernate 4.1
3. Persisting Object with Spring 3.1 and Hibernate 4
4. Spring 3 and Hibernate 4 for beginners


  1. Can u please post me source code to my email id...the above link is not working