General Info

For information on how to load the registry and on creating your own repository and registry loaders see the registry-cdc documentation.

Using the Hibernate Repositories Directly

This code sample shows how to use the Hibernate Repository to update the database. First, load the repository and then get the SessionFactory.

        RepositoryRegistry repositoryRegistry = RepositoryRegistry.Factory.create();
        repositoryRegistry.loadFromFile("/src/test/resources/hibernate2/sample-config.xml");
        Hibernate2Repository repository = (Hibernate2Repository) repositoryRegistry.find("hibernate2");

        SessionFactory factory = repository.getSessionFactoryByName("peer");
        Session session = factory.openSession();
	    Transaction transaction = session.beginTransaction();
        UserInfo info = new UserInfo();
        info.setFirstName("bart");
        session.save(info);
        transaction.commit();

            

The use Hibernate3 just replace Hibernate2Repository with Hibernate3Repository. In fact, you can use the registry to store both Hibernate2 and Hibernate3 repos concurrently.

Using the HibernateDAORegistry

Instead of dealing with the Hibernate Repositories directly, you can use the HibernateDAORegistry. The Registry allows you to find DAOs (a class instance implementing the org.jvending.registry.hibernate.DataAccessObject interface). The DAOs wrap around the one or more SessionFactories, completely encapsulting Hibernate from the rest of the application. Registry-J2SE takes care of setting everything up, provided that you provide the configuration file and implementation. A typical configuration file would look like:

    <registry-config>
       <repositories>
          <repository>
             <repository-name>hibernate2</repository-name>
             <repository-class>org.jvending.registry.hibernate.impl.Hibernate2RepositoryImpl</repository-class>

             <repository-config>${basedir}/src/test/resources/hibernate2/connections-hsqldb.xml</repository-config>
             <init-param>
                <param-name>dao:DAOTest</param-name>
                <param-value>org.jvending.registry.hibernate.test.DAOTestClass</param-value>
             </init-param>
             <init-param>
                <param-name>dao:AnotherDAOTest</param-name>
                <param-value>org.jvending.registry.hibernate.test.DAOTestClass2</param-value>
             </init-param>
          </repository>
        </repositories>
    </registry-config>
            

You can include one or more DAOs within the HibernateDAORegistry by creating a name-value pair, with dao:<unique-id> as the param-name and the implementing class as the param-value.

Unlike the Hibernate Repository example, the application developer does not need to bother with the Hibernate queries, since it is already contained with the DAO. To obtain the DAO:

            RepositoryRegistry repositoryRegistry = RepositoryRegistry.Factory.create();
            repositoryRegistry.loadFromFile("/src/test/resources/hibernate3/sample-config.xml");

            HibernateDAORegistry daoRegistry = HibernateDAORegistry.Factory.create();
            daoRegistry.setRepositoryRegistry(repositoryRegistry);

            DAOTestClass testClass =
                 (DAOTestClass) hibernateDAORegistry.find("dao:DAOTest");
            UserInfo info = new UserInfo();
            info.setFirstName("homer");
            testClass.update(info);
            

It is important to remember that prior to using the DAOS you must pass the RepositoryRegistry to the HibernateDAORegistry. The following is a sample DAO that you will find within the build download.

public class DAOTestClass implements DataAccessObject {

    private String className;

    private String id;

    private Map sessionFactories;

    private static Logger logger = Logger.getLogger("Hibernate");

    private HibernateRepository hibernateRepository;

    private RepositoryRegistry repositoryRegistry;

    public DAOTestClass(String className, String id) {
        this.className = className;
        this.id = id;
    }

    public void setSessionFactories(Map sessionFactories) {
        this.sessionFactories = sessionFactories;
    }

    public void setRepositoryRegistry(RepositoryRegistry repositoryRegistry) {
        this.repositoryRegistry = repositoryRegistry;
    }

    public void setHibernateRepository(HibernateRepository hibernateRepository) {
        this.hibernateRepository = hibernateRepository;
    }

    public UserInfo getUserInfoByName(String name) {
       String queryStatement =
            "select userInfo " +
            "from UserInfo as userInfo where first_name ='" + name + "'";
        Session session = null;
        try {
            session = getSessionFactoryByName("peer").openSession();
            Query query = session.createQuery(queryStatement);
            UserInfo info = (UserInfo) query.uniqueResult();

            if(info == null) {
                logger.log(Level.INFO, "Query failed: Query = " + queryStatement);
            }
            return info;

        } catch(Exception e) {
                logger.log(Level.INFO, "Query failed: Query = " + queryStatement, e);
        } finally {
            try {
                if(session != null) session.close();
            } catch(HibernateException e) {
                logger.log(Level.INFO, "Failed to close session", e);
            }
        }
        return null;
    }

    public void update(UserInfo userInfo) throws IOException {
        Session session = null;
        Transaction transaction = null;
        try{
            session = getSessionFactoryByName("peer").openSession();
            transaction = session.beginTransaction();
            session.save(userInfo);
            transaction.commit();
        } catch(Exception e) {
            try {
                if(transaction != null) transaction.rollback();
            } catch(HibernateException he) {
                logger.log(Level.WARNING, "Failed to roll back transaction", he);
            }
            logger.log(Level.INFO, "Rolled back transaction", e);
            throw new IOException("Could not update the database");
        } finally {
            try {
                if(session != null) {
                    session.clear();
                    session.close();
                }
            } catch(HibernateException e) {
                logger.log(Level.INFO, "Failed to close session", e);
            }
        }

    }

    private SessionFactory getSessionFactoryByName(String name) {
        Set keys = sessionFactories.keySet();
        for(Iterator i = keys.iterator(); i.hasNext(); ){
            String n = (String) i.next();
            if(n.equals(name)) return (SessionFactory) sessionFactories.get(n);
        }
        return null;
    }

    public String getClassName() {
        return className;
    }

    public String getID() {
        return id;
    }

}
            

Building Registry J2SE

Registry J2SE requires Registry-CDC 1.3.0 to run. Before building, you will need to download and build the CDC version.

Deploying

If you are only using Hibernate2Repository or the HibernateDAORegistry with DAOs only using Hibernate2, then you do not need to include Hibernate3 dependencies. If you are using both Hibernate versions, however, make sure that you use the dependent library versions specified for this project. Earlier versions of Hibernate2 dependencies may cause Hibernate3 to fail.