Mentawai Web Framework

Hibernate Integration

Like database connection pooling, this feature has nothing to do with a web framework. However, for easy integration, you can use the filter org.mentawai.filter.HibernateFilter that comes with Mentawai to work with Hibernate.

This filter saves the programmer from the repetitive task of configuring, initializing, opening and closing Hibernate sessions by providing a ready-to-use session to its action as an input parameter. The action does not even have to care about closing or returning the session. This is done transparently by the underlying filter.

By default, Mentawai configures the Hibernate SessionFactory using the Hibernate's default configuration, but you may specify the location of the Hibernate configuration file or giva a created SessionFactory, if you want.

The filter will inject the Hibernate Session in te action's input using "session" as the key by default, but you can change it with the method setKey().

From the version 1.4, The Hibernate Sessions are injected using the pull pattern. That means that they are injected only when you ask for it, invoking input.getValue("session") (or input.getValue() passing the key that you have configurated). Remembe that if you are using the InjectionFilter, it will call input.getValue("session") for you.

The inject session is open in the moment that it is requested and closed only after the complete request processing (involving the JSP page processing, for example), so you can you the Hibernate lazy loading.

Bellow there is an exemple of how a action uses the HibernateFilter:

import org.hibernate.Query;
import org.hibernate.Session;
import org.mentawai.filter.HibernateFilter;

public class ListUsers extends BaseAction {

	public String execute() throws Exception {
		Session session = (Session) input.getValue(HibernateFilter.KEY);
		Query q = session.createQuery("from User");
		input.setValue("users", q.list());
		return SUCCESS;



And to configure the HibernateFilter in the ApplicationManager, all you have to do is this:

public class ApplicationManager extends org.mentawai.core.ApplicationManager {        

    public void loadActions() {
        // Java-style
        addGlobalFilter(new HibernateFilter("/WEB-INF/hibernate/hiber.xml"));
        // Ruby-style
        filter(new HibernateFilter("/WEB-INF/hibernate/hiber.xml"));

Mentawai also provides a transaction management service, see this page for details: Transaction Filter