Table of Content
- Running the 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 .
- @Table(name = "EMPLOYEE")
This annotation defines which table inside the database this Object is to be mapped too.
- @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.
This annotation is used to define an Object property. Each properties map to a single field in the database.
- @Column (name="hire")
This annotation is used in combination with @Basic to force a property to be mapped to a different field. The omission of this annotation, in this example, would mean that this property would be mapped to the HireDate field in the database instead of the hire field. This would cause a RunTimeException to be thrown since this field does not exist in the database.
The HibernateHelper class is used to access the Hibernate Sessions. This class is copied from and often referred to in the Hibernate documentation.
This method adds package level metadata. This function is required for Hibernate Objects which have been mapped with hbm.xml files. These files where used prior to the release of annotations in Java 1.5 to define the mapping, and are not covered in this example.
This assigns persistent classes to the Hibernate session. This method must be called for each of the Objects that are to be used by this session.
Construct the SessionFactory which will provide Hibernate sessions to be used to retrieve and store objects in the database.
- return theSessionFactory.openSession();
Retrieve a Hibernate session to be used to retrieve and store Objects which were added to the Configuration.
The CreateEmployee class is a simple program which creates and saves 5 new Employees, and then outputs the number of Employees which are in the database. This is included in the package to demonstrate how the Employee class is to be stored and retrieved from the database using Hibernate.
- Employee theEmployee = new Employee();
Creating Hibernate Object and accessing its properties is done in the same manner as any other POJO.
- Session theSession = HibernateHelper.getSession();
A Hibernate session is required to store an Object.
This is the call used to save an Object to the database. Other functions such as update and delete are also supported.
- Criteria theCriteria = theSession.createCriteria(Employee.class);
Hibernate criteria are used to retrieve Objects from the database. The code above will retrieve all Employees in the database. The following code would return all Employee objects whose name starts with "Bob":
Criteria theCriteria = theSession.createCriteria(Employee.class)
.add( Restrictions.like("name", "Bob%") );
- List allEmployees = theCriteria.list();
The criteria.list() function executes the query on the database, and the results are returned in a java.util.List.
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. If you need more information about this file please refer to the following link.
- 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.