Integration between Hibernate and JTasks: Overview

Creating Date: 2005/July/26
Lastest Update: 2005/July/26

Let's talk about Hibernate and JTasks integration. Remember, if you want to learn more about Hibernate, take a look at the official document ( Hibernate is an excellent persistence framework which helps on the work with database. Hibernate supports a lot of relational database, by default, JTasks comes with mySQL support (you can change this support easily, take a look in the install sections to know how can you do that). If you don't see hibernate working yet and want to change modification in JTasks, I advise you to see hibernate document firstly, build some examples to after trying to change JTasks.

Configuration File

Hibernate can works with two kinds of configuration file, file or hibernate.conf.xml. JTasks uses hibernate.conf.xml, but why? Because into hibernate.conf.xml file you can put a lot of information, such as: What database you will use, username, password, JDBC driver and the biggest difference than, it allows you add hibernate map file too (*.hbm.xml). At the most cases you will need to make bit modifications into this file, these modifications are explained in install section. See the example bellow:

Now, let's talk about map files. These files are the Java classes represented in one XML file. These files receive the extension hbm.xml and must be added into hibernate.conf.xml. Each java class model will have yourself hbm.xml file showing the primary key attribute, relationship and each attribute type. Bellow you can see one example.

The example above is about the class. You can see in the hbm.xml file that the primary key is represented and the other attributes have your kinds defined. It's important to know that each Java class have your own hbm.xml file and these files are localized in the same package from Java class, in JTasks, this package has jtasks.model name.

It seems hard to create this hbm.xml file by hand, I agree, so in the JTasks we are using ant+xdoclet to generate these file for us. There is the hibernate-xdoclet.xml in the root project, this is a ant+xdoclet file already read and when you want to create/modify a hbm.xml file, you can run this file using ant, it's simple and fast =).

Model Package

All java model files are localized inside jtasks.model package. To ant+xdoclet generate the hbml.xml file, you need to put some special "tags" inside this java class. Look the image bellow to see how the Login class works.

Look the tags started with @hibernate string. If you want to learn more, please take a look at the official document.

DAO Package

At first, you should to know which DAO is a design pattern, used in database functions. Hibernate works with DAO concept as well. In JTasks project, we have a specified package to put the DAOs files jtasks.dao. The principal file there is the file, it has one important Object, called SessionFactory. This object must be started only once (when the application server starts) and used in all system, for that, we use singleton pattern. file is the top of hierarchy file inside jtasks.dao package. ALL other DAO (e.g. LoginDAO, ProjectDAO) must inherent from DAO, thus all DAOs files will have SessionFactory object.

To start the SessionFactory, one method called start() is implemented. You likely won't need to change this method. Other methods also are implemented, such as: insert, delete, update, getList and so fourth, these methods can be used to a lot of models.


As we said later, the SessionFactory object must be initialized when the application server starts, but how can we do that? Simple answer, to resolve this situation, we built a servlet called localized inside jtasks.system package. This servlet has init() method that is performed when the servlet is executed. Into this method we call DAO.start() method. Up to now there is no secret, but you can ask to me, how can I do to Initialization starts when the application server starts? The answer is simplest, only put the 1 tag into web.xml file. Don't you believe? I'm not kidding, look for the web.xml file from JTasks and you will see a line looks like this: