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.
@Entity
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) {
this.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 {
@Override
public Class getHomePage() {
return TheWall.class;
}
@Override
protected void configureHibernate(AnnotationConfiguration config) {
super.configureHibernate(config);
config.addAnnotatedClass(Graffito.class);
}
}
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"));
@Override
protected void onSubmit() {
super.onSubmit();
clearPersistentObject();
setResponsePage(OrderList.class);
}
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) {
criteria.addOrder(Order.desc("id")).setMaxResults(5);
}});
// show previous scrawls in list
add(new PropertyListView("graffiti", lastFive) {
@Override
protected void populateItem(ListItem item) {
item.add(new RenderedLabel("text", true)
.setFont(spidershank)
.setColor(orange));
}});
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.
2 comments:
Markus, thanks for this article, however I cannot resists to add that Wicket also supports iBatis aside of Databinder for Hibernate.
yep, it supports ibatis. we chose that at my workplace, and i personally prefer ibatis to hibernate now.
Post a Comment