Directory

← Back

HbnContainer

HbnContainer is a data connector for Vaadin based on Hibernate. It is used for binding entities with database records to feed components like tables, forms and trees.

Author

Contributors

Rating

** Please note ** This project is inactive and in need of a maintainer. If you would like to take it over please fork it on github and add the support and features that you need. Because of its popularity I don't want to just take the codebase offline so if anyone has interest please take it over for the benefit of the community.

HbnContainer is a data connector for Vaadin based on the popular Hibernate ORM from JBoss. It is distributed as a Vaadin Add-on and it is used for binding entities with database records to feed components like tables, forms and trees.

Compatibility:

  • Vaadin 6 + Hibernate 3.x: HbnContainer 1.0
  • Vaadin 6 + Hibernate 4.x: HbnContainer 1.1
  • Vaadin 7 + Hibernate 4.x: HbnContainer 2.0

License:

  • HbnContainer is licensed under the Apache 2.0 License.
  • You are free to use HbnContainer in commercial applications.

Notes:

  • I will maintain a branch for 1.1 code for bug fixes
  • I currently have no plans to add features to 1.1
  • New features will be added to 2.x only
  • Please use the project page for reporting bugs
  • Please use the project page for feature requests
  • Please contribute any enhancements back to the community

Origins: HbnContainer originated with Matti Tahvonen at Vaadin who created it for an article explaining how to use Hibernate with Vaadin. I took over as maintainer in 2012 when Matti found that he didn't have time to maintain this project along with all the other work he was doing for Vaadin and for the open source community. Matti continues to advise and consult on this project occasionally, as needed.

Sample code

table.setContainerDataSource(new HbnContainer(Workout.class, this));
public class HibernateServletFilter implements Filter
{
	private static final Logger logger = LoggerFactory.getLogger(HibernateServletFilter.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException
	{
		logger.debug("Initializing HibernateServletFilter");
	}

	@Override
	public void destroy()
	{
		logger.debug("Destroying HibernateServletFilter");
		final Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
		if (session.getTransaction().isActive())
		{
			logger.debug("Committing the final active transaction");
			session.getTransaction().commit();
		}

		if (session.isOpen())
		{
			logger.debug("Closing the final open session");
			session.close();
		}
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
	{
		final Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		try
		{
			logger.debug("Starting a database transaction");
			session.beginTransaction();
			
			chain.doFilter(request, response);
			
			logger.debug("Committing the active database transaction");
			session.getTransaction().commit();
		}
		catch (StaleObjectStateException e)
		{
			logger.error(e.toString());

			if (session.getTransaction().isActive())
			{
				logger.debug("Rolling back the active transaction");
				session.getTransaction().rollback();
			}
			
			throw e;
		}
		catch (Throwable e)
		{
			logger.error(e.toString());
			
			if (session.getTransaction().isActive())
			{
				logger.debug("Rolling back the active transaction");
				session.getTransaction().rollback();
			}
			
			throw new ServletException(e);
		}
	}
}
<filter>
	<filter-name>HibernateServletFilter</filter-name>
	<filter-class>com.vaadin.demo.HibernateServletFilter</filter-class>
</filter>
<filter-mapping>
	<filter-name>HibernateServletFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>
public class HibernateUtil
{
	private static final Logger logger = LoggerFactory.getLogger(HibernateUtil.class);
	private static SessionFactory sessionFactory;

	static
	{
		try
		{
			logger.debug("Initializing HibernateUtil");
			
			final Configuration configuration = new Configuration();
			configuration.configure();
			
			final ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder();

			final ServiceRegistry serviceRegistry = serviceRegistryBuilder
					.applySettings(configuration.getProperties())
					.buildServiceRegistry();

			sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		}
		catch (Throwable e)
		{
			logger.error(e.toString());
			throw new ExceptionInInitializerError(e);
		}
	}

	public static SessionFactory getSessionFactory()
	{
		return sessionFactory;
	}
}

Links

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

Released
2010-01-22
Maturity
BETA
License
Apache License 2.0

Compatibility

Framework
Vaadin 6.1+
Vaadin 6.0+ in 1.0.1
Vaadin 7.0+ in 2.0.0
Browser
N/A
Online