WEBINAR:
On-Demand
Application Security Testing: An Integral Part of DevOps
The Hibernate Configuration File
You can configure the Hibernate environment in a couple of ways. One standard way that proves very flexible and convenient is to store the configuration in a file named
hibernate.cfg.xml. You place the configuration file at the root of a Web application's context classpath (e.g.
WEB-INF/classes). You then access the file and read it using the
net.sf.hibernate.cfg.Configuration class at runtime.
The hibernate.cfg.xml file defines information about the database connection, the transaction factory class, resource mappings, etc. The following code demonstrates a typical configuration file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:data/userejb</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="show_sql">true</property>
<property name="dialect">net.sf.hibernate.dialect.HSQLDialect</property>
<property name="transaction.factory_class">
net.sf.hibernate.transaction.JDBCTransactionFactory
</property>
<mapping resource="com/jeffhanson/businesstier/model/UserInfo.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The Hibernate Mapping Configuration File
Hibernate applications make use of mapping files containing metadata defining object/relational mappings for Java classes. A mapping file is designated with a suffix of
.hbm.xml. Within each configuration file, classes to be made persistent are mapped to database tables and properties are defined which map class-fields to columns and primary keys. The following code illustrates a typical Hibernate configuration file named
UserInfo.hbm.xml:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.jeffhanson.businesstier.model.UserInfo" table="USER">
<id name="id" type="string" unsaved-value="null" >
<column name="USER_ID" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<property name="fullName">
<column name="FULLNAME" length="32" not-null="true"/>
</property>
<property name="address"/>
<property name="city"/>
<property name="state"/>
<property name="zip"/>
</class>
</hibernate-mapping>