Monday, 8 April 2013

ZK MVC CRUD With Spring 3 + JPA + Hibernate 4 Entity Manager

A simple CRUD Application based on JPA.

Overview
This article will explain you step by step how to create simple CRUD application using ZK as Presentation layer with Spring 3 and JPA (Hibernate vendor).

What is JPA and why JPA
The Java Persistence API, referred as JPA is a object-relational mapping solution to enterprise Java applications.
JPA is a spec and not an implementation, so if you choose JPA, you still have to pick an implementation. You can pick Hibernate, Top link, and any other ORM that implements JPA.

Using JPA does not tie you to any particular ORM Like hibernate, toplink, etc. EntityManager provides vendor independent access to persistence. Portability across application servers and persistence products (avoids vendor lock-in).

Developers can choose the best ORM implementation according to the application requirement. For example, production can be started from the free versions of ORM implementation and when the needs arise it can be switched to the commercial version of the ORM framework. You can switch the persistence provides without changing the code. So, ORM framework independence is another another big benefit of JPA

Hibernate ORM
In this example, we are going to choose Hibernate as ORM vendor to build the simple CRUD Application.

Spring and Hibernate ORM Framework Integration.
There are lot of articles on the net about this topic. I recommend to the read following articles on this subject.

1. http://www.javabeat.net/2007/10/integrating-spring-framework-with-hibernate-orm-framework/
2. http://static.springsource.org/spring/docs/2.5.x/reference/orm.html
3. http://blog.springsource.org/2012/04/06/migrating-to-spring-3-1-and-hibernate-4-1/
4. http://javaprogrammingtips4u.blogspot.in/2010/04/spring-integration-with-hibernate_23.html

Technologies used in this example:

1. ZK 6.5.1 CE Version.
2. Hibernate-entitymanager 4.1.9 Final
3. Spring-orm 3.2.0 Release
4. Spring-web 3.2.0 Release
5. Java mysql connector 5.1.22

In mysql, create the following table

image
Part 1: First let us setup the development environment. Download this document and follow the step by step instruction to install the necessary software’s.

Part 2: This post explain how to create ZK Maven project in eclipse IDE as step by step.
Part 3: This post explain how to configure database, user name, password, etc using persistence.xml file
Part 4: This post explain how to integrate Spring and Hibernate.
Part 5: This post explain how to create DAO and service layer for the CRUD operation.
Part 6: This post explain how to create Presentation layer using ZK Framework.


Output before with ZK Default CSS Style

image

image

Output after defining our own CSS for each component.

image

image

Video demo:
http://screencast.com/t/DFRiwmpNHq

You can download the source code here

MVVM Version of this tutorial is available here

13 comments:

  1. Excelent Work!, we use a similar Stack ejb3+zkoss over jboss 7.1. and inject the ejb3 with spring over zkoss. also use springsecurity with ACL.

    Regards

    ReplyDelete
  2. very nice sir ... I am a student and new to zk framework, the pdf you created is awesome helped me a lot.. thank you much much thank you... :) :)

    ReplyDelete
  3. hi can you help me to understand why am getting this error message when i run the program pleae "CRUDExample.MyViewModel"

    ReplyDelete
  4. Nice article for a new comer to learn Zk,Spring and Hibernate together

    ReplyDelete
  5. Replies
    1. Thank you sudarshan. I am extremely happy for being great people like you visiting my blog and giving feedback.

      Delete
  6. Hello and thank you for the Tutorials. Can you help me with an error? I tried following this example but got an error when started the app in Tomcat. I went back and downloaded your source file and get the same error.

    The Tomcat log can be found here:
    http://pastebin.com/ASQRXAx0

    Can you help me identify what is wrong?

    ReplyDelete
  7. Hi..nice work !

    I have te follow error:

    GRAVE:
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
    at org.zkoss.zkplus.spring.SpringUtil.getApplicationContext(SpringUtil.java:42)
    at org.zkoss.zkplus.spring.SpringUtil.getBean(SpringUtil.java:52)
    at crudexample.controller.UserListContrroller.doFillListbox(UserListContrroller.java:66)
    at crudexample.controller.UserListContrroller.onCreate$userList(UserListContrroller.java:59) ...

    Can you help me?

    []s

    ReplyDelete
  8. Thank you for your article it’s looks good and providing some valuable information

    ReplyDelete
  9. Thank you,so thank of this tutorial! Hope you have more the lession like this!AWS Training
    Thank agains!

    ReplyDelete
  10. Hola buenas tardes.
    * Estoy trabajando con MVC tengo los iguientes archivos.
    * Vista1.zul
    * controlador
    @VariableResolver(DelegatingVariableResolver.class)
    public class Vista1_Controller extends SelectorComposer{
    ...
    }
    * Vista2.zul
    @VariableResolver(DelegatingVariableResolver.class)
    public class Vista2_Controller extends SelectorComposer{
    ....
    }
    en la vista 1 tengo un combo, panel, boton de guardar,
    el el evento del combo
    en el panel pinto una vista de la siguiente forma
    Executions.getCurrent().createComponents(zul, panelLE.getPanelchildren(), arguments);
    y hasta ahi no hay ningun problema,
    En el evento del boton de guardar tengo que llamar un metodo que esa en el cntrolador de la vista 2
    y lo estoy haciendo de la siguiente forma
    Vista2_Controller vista2_Controller = new Vista2_Controller();
    vista2_Controller.obtenerObjeto();

    donde le metodo obtenerObjeto() me tiene que renornar un objeto que se llena con los
    datos que tiene la vista pero al hacer este llenado
    no encuentra los campos de la vista.
    y me muestra un error
    nullpointerexception null
    el error esta en esta linea al querer
    obtener el valor del campo
    catCarro.setNumeroLlanastas (txtbLlantas.getvalue());
    y el campo si tiene dato

    public CatCarro obtenerObjeto(){
    CatCarro catCarro = new CatCarro();
    catCarro.setNumeroLlanastas (txtbLlantas.getvalue());
    ...
    return catCarro();
    }

    ReplyDelete
  11. It fun to have a content like this really worth to read it
    AWS Training

    ReplyDelete
  12. Error: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context:

    ReplyDelete