Hibernate: My Spring-Hibernate 101

Posted on January 18, 2010


It has been a while I work with Spring Framework but I have yet touch Hibernate. Today is the first time I am trying to look at Hibernate in detail in a project that I lead. This project has been around for about a year plus; it is a website powered by Servlets using Spring Framework and Hibernate, which is mainly written by a friend.

At the first glance, Hibernate looks very confusing but after reading it for a while, I finally got the pattern. Hibernate has, at least in my project, 4 files:

  • Something.hbm.xml
    This is the first file that has to be created when a table is going to be included in the hibernate setting. This is an XML file, following the hibernate standard. It contains the information regarding the table columns.
  • Something.java
    Once Something.hbm.xml exists, a java class, containing the table columns as attributes, is created. This class contains only the setter and getter to the attributes.
  • SomethingDao.java
    Unlike Something.java, SomethingDao.java is a Data Access Object interface (DAO). This interface lists down all functions that are able to make changes in the table.
  • SomethingDaoImpl.java
    Finally, this last file is an implementation of SomethingDao.java interface.

I also learn that Data Access Object (DAO) has to be declared in the servlet configuration to let it be loaded during start up. These DAOs are shared among controllers to alter database contents. Additionally Hibernate SessionFactory, which requires DataSource and HibernateProperties, are included in the servlet configuration.

Posted in: MyWay, Technology