Table of Content
- Running the example
This Hibernate example maps two Java Objects (Employee.java and Task) to tables in a relational database. This example also creates some instances of these Objects and saves them into a database.
The following diagram list the four classes which make up this example.
The Employee class is a simple POJO which has been setup to be able to be used as an Hibernate Object.
Hibernate uses Sun's EJB3 Annotation standard to map persistent Objects. Because of this, all Hibernate Objects are treated as Entity Beans.
Note: Although Hibernate uses the EJB3 standard in this manner, the Objects do not need to be deployed in an EJB container to be used. An interesting benefit of this is that no modifications would be required for this class to be used inside a J2EE compliant server as an Entity Bean .
- @Id(generate = GeneratorType.AUTO)
This annotation defines the primary key for this Object. The following key generators can be used for generating IDs: AUTO, TABLE, IDENTITY, SEQUENCE, or NONE.
- @OneToMany (mappedBy = "employee")
This annotation is used to define a one-to-many relationship with an other Object. The "mappedBy" field is the foreign key of the Object.
- @OrderBy ("name")
This annotation is used in combination with @OneToMany to define that the collection is to be ordered. In this example the collection will be ordered by the "name" field in the Task table.
By default all functions inside an EJB are persistent, and therefore are mapped to a field in the database. However, in this case both the getCompletedTasks() and getToDoList() are not persistent fields.
The Task class is a simple POJO which has been setup to be able to be used as an Hibernate Object.
This annotation is used to define a many-to-one relationship with an other Object.
The HibernateHelper class is used to access the Hibernate Sessions. This class is copied from and often referred to in the Hibernate documentation.
- .setProperty("hibernate.hbm2ddl.auto", "create")
The "hibernate.hbm2ddl.auto" will reload the database tables every time the session factory is built. This is an useful method to test the data element structure. Programmers should remove this before deploying the program.
The CreateEmployee class is a simple program which creates and saves 5 new Employees, 15 completed tasks, 10 uncompleted yet assigned tasks, and two unassigned tasks. The program then outputs the information that is located in the database. This is included in the package to demonstrate how the Employee and Task classes are stored and retrieved from the database using Hibernate.
The last component for this example is the Hibernate configuration file, which is required for the communicate with the database. Information such as the database location, user-name, and password are entered in this file.
- Download and install Sun's JDK 1.5 if it is not already installed
- Download and install the Apache's Ant build tool if it is not already installed
Once the server is install complete the following commands to load the database schema into the database.