O/R Mapping with Hibernate, Page 2
Hibernate can be configured by creating a property file named hibernate.properties in the src directory and adding its path to the application's classpath. This file consists of the properties used by Hibernate to connect to database, generate schema, and obtain other database-specific information. To reflect changes in the underlying database into the whole application, only values of the properties in this file need to be modified. Listing 1 shows a simple example. Most of these properties are self-explanatory.
Note: Now Hibernate also can be configured by using a simple XML file named hibernate.cfg.xml, which exists inside the src directory. This file's structure is very similar to hibernate.properties and has the same functionality. Listing 2 gives an example.
Building an Application with Hibernate
Now, we are ready to build the application. For that, the following steps need to be taken.
- Creating mapping documents
- Generating stub Java classes for persistent objects
- Generating database schema
- Preparing code to initialize and run Hibernate in an appropriate place
Creating Mapping Documents
Mapping documents are XML documents used to define the persistent objects and contain information about an object's persistent fields, associations, subclasses, and proxies, if any. One mapping document is created for each persistent object and saved in a file with the name class_name.hbm.xml, where class_name is the name of the object's class. Listing 3 gives an example of mapping document Event.hbm.xml.
The mapping documents are compiled at application start-up to provide Hibernate with information about the persistent objects' corresponding classes, their respective structures, to which database table should they be mapped, and how. Hibernate also uses these mapping documents to generate corresponding database schema and stub Java classes for the persistence layer, using inbuilt utilities called SchemaExport and CodeGenerator, respectively.
Generating Stub Classes
This task becomes simpler after mapping documents are created. Stub classes can be created by using Hibernate's built-in utility CodeGenerator by executing a simple command. Command's syntax is given below:
java -cp classpath net.sf.hibernate.tool.hbm2java.CodeGenerator options mapping_files
Provide appropriate values for the classpath, options, and mapping_files parameters. Listing 4 shows the stub file generated using the mapping document given in Listing 3.
Generating Database Schema
To generate database schema using Hibernate's SchemaExport, execute the following command after substituting appropriate values for parameters:
java -cp classpath net.sf.hibernate.tool.hbm2ddl.SchemaExport options mapping_files
Provide appropriate values for the classpath, options, and mapping_files parameters. Figure 1 shows the schema generated using the mapping document given in Listing 3.
Figure 1: Graphical representation of schema generated using the mapping document in Listing 3
Initializing and Running Hibernate
To initialize and run hibernate, the following steps are to be taken:
- Inside an appropriate class, instantiate and populate the desired object to be persisted.
- Obtain the net.sf.hibernate.SessionFactory object using the net.sf.hibernate.cfg.Configuration object at the start of the application.
- Open net.sf.hibernate.Session by calling the openSession() method on the SessionFactory object.
- Save the desired object and close the Session.
Listing 5 shows how to implement the steps described above using a simple class. Now, the application is complete and, when executed, saves the desired objects to the underlying database, i.e. makes them persistent.
Hibernate is a powerful, high-performance, feature-rich and very popular ORM solution for Java. Hibernate facilitates development of persistent objects based on the common Java object model to mirror the underlying database structure. Along with mapping objects to a database, Hibernate also provides advanced data query and retrieval services through HQL, efficient caching, and other optimization techniques and useful built-in utilities for code and schema generation.
By automating the generation of a persistence layer to a large extent, Hibernate helps relieve the developer of up to 95% of common persistence-related coding.
About the Author
Mugdha Chauhan (formerly Mugdha Vairagade) is a senior IT consultant and author. An Open Source supporter, she frequently writes articles and tutorials on useful emerging Open Source projects. Major tech portals including developer.com, IBM developerWorks, CNET Networks, Slashdot, and many eZines regularly publish her work. Her expertise and interests include Java, Linux, XML, wireless application development, and Open Source.
Page 2 of 2