Configuring Hibernate for a Java Servlet Application
by
Jeyarani Venkatasamy
Most enterprise applications have to interact heavily with their back-end databases. In order to facilitate these interactions and make them efficient and fast, enterprise application developers create a persistence layer or database integration layer between the application and the underlying database. These layers store and retrieve data between the application and the database, as well as update and delete it. In J2EE/Java EE-based enterprise applications, the persistence layer consists of Java classes that map objects to data and vice versa. This layer is often built using JDBC, entity beans, JDO, and so forth.
From an object-oriented perspective, persistence actually can be considered an extension of an object's lifecycle -- a minor part of the software, as compared with the whole domain. Thus, a programmer who relies on a database management system (DBMS) should not focus mainly on persistence, because DBMSs are complex entities with their own constraints, rules and protocols that the programmer needs to follow. In order to follow DBMS constraints, rules and protocols; programmers -- even object-oriented programmers -- are tempted to put their main focus on the database technology and persistence and start performing data processing.
Object relational modeling (ORM) tools such as Hibernate and TopLink allow transparent persistence that enables applications to switch to any database. And also it overcomes the paradigm mismatch between object-oriented data and table-oriented relational databases.
Hibernate 3.0, the open source persistence technology at the heart of EJB 3.0, is available for download from Hibernate.org. Hibernate maps Java classes to database tables. It also provides data query and retrieval facilities that significantly reduce development time. Hibernate is not the best solution for data-centric applications that use only stored procedures to implement business logic in databases. It is most useful with object-oriented domain modes and business logic in Java-based middle tiers. Hibernate allows transparent persistence that enables applications to switch to any database. Hibernate can be used in Java Swing applications, Java Servlet-based applications, or J2EE/Java EE applications using EJB session beans.
In this article, we'll cover configuring Hibernate for a Web application to persist the Employee persistent class. More specifically, we'll discuss the following:
- Creating a basic hibernate.cfg.xml file
- Building mapping definition files to provide Hibernate with information about the persistent class (Employee class).
- The primary Hibernate classes used to persist and retrieve classes
Assumptions
- Ant, Hibernate, Oracle and WebLogic server are installed correctly.
- Basic knowledge of XML for the configuration file and mapping documents
- Download this basic project.
Configuring Hibernate
Hibernate provides two alternative configuration files: a standard Java properties file called hibernate.properties and an XML formatted file called hibernate.cfg.xml. We'll use the XML configuration file in this article, but it's important to understand that both configuration files perform the same function: configuring the Hibernate service.
Before configuring a Hibernate service, we should first determine how the service obtains database connections. Database connections may be provided by the Hibernate framework or from a JNDI DataSource.
The sample configuration file in Listing 1 uses Hibernate-managed JDBC connections. We would typically encounter this configuration in a non-managed environment, such as a standalone application.
Listing 1. Example hibernate.cfg.xml File
<!--l version='1.0' encoding='utf-8-->
<!--CTYPE hibernate-configuration PUBLIC <-->
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
true
org.hibernate.dialect.OracleDialect
oracle.jdbc.driver.OracleDriver
jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1522))(CONNECT_DATA = (SID = TESTDB)))
oracle
oracle
To use Hibernate-provided JDBC connections, the configuration file requires the following five properties:
connection.driver_class -- The JDBC connection class for the specific database
connection.url -- The full JDBC URL to the database
connection.username -- The username used to connect to the database
connection.password -- The password used to authenticate the username
dialect -- The name of the SQL dialect for the database
These connection properties are very familiar to any Java developer who has worked with JDBC in the past. Because we're not specifying a connection pool, Hibernate uses its own basic connection-pooling mechanism.
The dialect property is org.hibernate.dialect.OracleDialect, which tells Hibernate that we are using an Oracle database. Hibernate ships with more than 15 SQL dialects, supporting each of the major database vendors, including Oracle, DB2, MySQL and PostgreSQL. With Hibernate, we can use the following databases dialect type property, which is the mapping for our Employee table.
Database Name
|
SQL dialect
|
|
DB2
|
org.hibernate.dialect.DB2Dialect
|
|
HypersonicSQL
|
org.hibernate.dialect.HSQLDialect
|
|
Informix
|
org.hibernate.dialect.InformixDialect
|
|
Ingres
|
org.hibernate.dialect.IngresDialect
|
|
Interbase
|
org.hibernate.dialect.InterbaseDialect
|
|
Pointbase
|
org.hibernate.dialect.PointbaseDialect
|
|
PostgreSQL
|
org.hibernate.dialect.PostgreSQLDialect
|
|
Mckoi SQL
|
org.hibernate.dialect.MckoiDialect
|
|
Microsoft SQL Server
|
org.hibernate.dialect.SQLServerDialect
|
|
MySQL
|
org.hibernate.dialect.MySQLDialect
|
|
Oracle (any version)
|
org.hibernate.dialect.OracleDialect
|
|
Oracle 9
|
org.hibernate.dialect.Oracle9Dialect
|
|
Progress
|
org.hibernate.dialect.ProgressDialect
|
|
FrontBase
|
org.hibernate.dialect.FrontbaseDialect
|
|
SAP DB
|
org.hibernate.dialect.SAPDBDialect
|
|
Sybase
|
org.hibernate.dialect.SybaseDialect
|
|
Sybase Anywhere
|
org.hibernate.dialect.SybaseAnywhereDialect
|
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.