Monday, August 27, 2007

[Tech] Combine Hibernate and Wicket

In the previous post, Alex gives an overview about Wicket and what advantages and disadvantages this component based framework has. The majority of web applications are very data intensive, where simple CRUD (Create, Read, Update, Delete) operations needed. Apart from this, many software projects use the popular ORM (Object Relational Mapping) Tool Hibernate in association with annotated domain objects. Such a tool close (or better "try" to close) the gap between the object oriented world and the relational world. To combine these two different worlds is not a trivial task. Consider object oriented appraches like inheritance, polymorphism and other points.

Back to the blog issue! Wicket is an component oriented web framework, commemorating on Swing. On the back end you have Hibernate, often used to implement the DAOs in an application. Currently finished a data driven web application, using Databinder, a simple bridge from Wicket to Hibernate. As a classical MVC framework, Wicket uses models in order to populate data in forms and vice versa. What are the most uses cases of data driven solutions:
  • Show a list of data
  • Filter a list of data
  • Show details of data
  • CRUD operations of objects
  • and some other features
Databinder is a library providing different types of wicket models and view components enables an easy Hibernate integration. For example a data form, containing a number input components for the user and a submit button. Submitting the form entails a model update in the database out of the box. Another use case is to set up a query (Hibernate Query) to receive a list of objects. The list is wrapped in a HibernateListModel which can be populated in an easy way. In order to illustrate the main functionality of databinder, consider the following code snippet, taking from the databinder examples.
public class Graffito implements Serializable {
private Integer id;
private String text;
public String getText() {
return text;
public void setText(String text) {
this.text = text;
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
public void setId(Integer id) { = id;

Each databinder application must provide an implementation of the DataApplication, which enhances the WebApplication object from wicket. Databinder must know about the managed annoated data objects, by add all annotated classes to the applicaiton.
public class GraffitiApplication extends DataApplication {
public Class getHomePage() {
return TheWall.class;

protected void configureHibernate(AnnotationConfiguration config) {

Now databinder is ready to use and manage your domain objects in components, like DataForm, DataPanels, Tables and the like. The following code snippet provides a data form to display domain object details.
    public class MyForm extends DataForm {
public MyForm(String id) {
super(id, Graffito.class);
add(new TextField("text"));
protected void onSubmit() {

Databinder provides a HibernateListModel enables the developer to create list models by using the powerful Criteria API of Hibernate or Hibernate Query Language. The generated list model can then be populated in a list, by using property list view for instance.
IModel lastFive = new HibernateListModel(Graffito.class, new ICriteriaBuilder() {
public void build(Criteria criteria) {

// show previous scrawls in list
add(new PropertyListView("graffiti", lastFive) {
protected void populateItem(ListItem item) {
item.add(new RenderedLabel("text", true)
The aim of this blog was not to introduce in databinder, rather I want to give a short overview of an interesting integration library between wicket and hibernate.


Alexander Schatten said...

Markus, thanks for this article, however I cannot resists to add that Wicket also supports iBatis aside of Databinder for Hibernate.

jonathan said...

yep, it supports ibatis. we chose that at my workplace, and i personally prefer ibatis to hibernate now.