Dashboard > XWork > Documentation > XWork Hibernate Integration
  XWork Log In View a printable version of the current page.  
  XWork Hibernate Integration
Added by tm_jee, last edited by tm_jee on Nov 11, 2006  (view change)
Labels: 
(None)

XWork2 could have Hibernate integration as well. However such an integration is not build in by default. In other words, it doesn't come bundled with XWork2 itself.

Senario

Typical senario would be to have Hibernate session prepared before the action is actually executed and clear after the action and result are done executing. This could be handled by a XWork2 interceptor.

Code

public class HibernateInterceptor implements Interceptor {
    ...
    public String intercept(ActionInvocation invocation) throws Exception {
       Session session = null;
       try {
           // please refer to Hibernate site for imlpementation of HibernateUtil (www.hibernate.org)
           session = HibernateUtil.getSessionFactory().getCurrentSession();
           session.beginTransaction();
           Object action = invocation.getAction();
           if (action instanceof HibernateSessionAware) {
              ((HibernateSessionAware) action).setHibernateSession(session);
           }
           invocation.invoke();
           session.getTransaction().commit();
       }
       catch(Exception e) {
           exceptionHandler(e);
           session.getTransaction().rollback();
       }
    }
    ...
    /**
     *  Hook for subclass to handle exception.
     */
    protected void handleException(e) {
        // maybe we could do better than this. 
        LOG.error(e.toString(), e);
    }
    ...
 }
public interface HibernateSessionAware {
      void setHibernateSession(Session session);
  }

The above is just a simple code to show one possibility of how Hibernate / XWork2 integration could be done.

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators