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:
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.
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.
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.
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.