Mentawai supports Hibernate the same way it supports a connection to the database, but instead of configuring a connection handler you setup a Hibernate session handler in the application manager.
Setting up the session handler in the application manager:
@Override public SessionHandler createSessionHandler() { Props props = getProps(); hibernateConfig = new Configuration(); hibernateConfig.addAnnotatedClass(User.class); // register your annotated beans here... hibernateConfig.setProperty("hibernate.dialect", props.getString("hibernate.dialect")); hibernateConfig.setProperty("hibernate.connection.driver_class", props.getString("jdbc.driver")); hibernateConfig.setProperty("hibernate.connection.url", props.getString("jdbc.url")); hibernateConfig.setProperty("hibernate.connection.username", props.getString("jdbc.user")); hibernateConfig.setProperty("hibernate.connection.password", props.getString("jdbc.pass")); hibernateConfig.setProperty("hibernate.show_sql", props.getString("hibernate.show_sql")); return new SessionHandler(hibernateConfig.buildSessionFactory()); }
Using it:
The session handler works as a session pool and it is fully integrated with the IoC container so you can get a session from anywhere. You also need not worry about closing the session because this is done automatically by the container in the thread local scope.
// inside the action you can just do: Session session = (Session) input.getValue("session");
DI and Auto-wiring:
The session will be injected automatically in any component that depends on it, by setter or constructor. For example, to write a DAO that depends on the hibernate session all you have to do is include a session parameter in the constructor:
public class HibernateUserDAO implements UserDAO { private final String blah; private final Session session; private final int foo; public HibernateUserDAO(String blah, Session session, int foo) { this.blah = blah; this.session = session; this.foo = foo; } }
Getting a connection to the database:
The session handler also has a connection handler inside of it, so when you set a session handler you are also setting a connection handler. Therefore you have the same features you have with a DB Connection and you can get and inject a connection from and to anywhere.
// in the action: Connection conn = (Connection) input.getValue("conn"); // through IoC: public class JdbcUserDAO implements UserDAO { private final String blah; private final Connection conn; private final int foo; public JdbcUserDAO(String blah, Connection conn, int foo) { this.blah = blah; this.conn = conn; this.foo = foo; } }
NOTE: The connection provided by the session handler will be unrelated to any other session provided by the session handler. If you want to get the connection from a specific session, read below.
Getting around the deprecated session.connection() method:
Hibernate has decided to deprecate the session.connection() method. To work around that you can use the method below from the HibernateUtils class provided by Mentawai:
// get the connection from the session: Connection conn = HibernateUtils.getConnectionFrom(session);