Hibernate provides transparent mapping between a persistence class and a relational database using an XML mapping file. The actual storing and loading of objects of the persistence class is based on the mapping metadata. Perform the following steps to accomplish this:
The New Hibernate XML Mapping files wizard gets started. As we have not yet defined any persistence classes to map, we will first create an empty XML mapping file.
The resources in the src/main/resources
directory are in the classpath of a Hibernate application.
jboss-hibernate
| src
| main
| resources
folder and specify File name as catalog.hbm.xml
, as shown in the following screenshot. Click on Finish.The catalog.hbm.xml
mapping file gets added to the resources
directory. The root element of the mapping file is hibernate-mapping
. The persistence classes are configured using the <class/>
element. Add a <class/>
element to the org.jboss.hibernate.model.Catalog
class specified with the name
attribute in the <class/>
element. We have yet to create the persistence class, which would not be required if we were just exporting a schema to a relational database but is required to store or load any POJO objects. Specify a table that the class is to be mapped to with the table
attribute of the <class/>
element. With the specified mapping, an instance of the Catalog
class is mapped to a row in the CATALOG
database table. A mapped persistence class is required to specify the primary key column of the table it is mapped to. The primary key column is mapped to an identifier property in the persistence class. The primary key column and the identifier property are specified using the <id/>
element. The column attribute specifies the primary key column; the name attribute specifies the identifier property in the persistence class being mapped; and the type attribute specifies the Hibernate type. The <generator/>
subelement of the <id/>
element specifies the primary key generation strategy. Some built-in generation strategies are available and different relational databases support different ID generation strategies.
As we are using the MySQL database, which supports identity columns using AUTO_INCREMENT
, we can use the generation strategy as identity
or native
. An identity column is a table column of the INTEGER
type, with AUTO_INCREMENT
and PRIMARY KEY
or UNIQUE KEY
specified, such as id INTEGER AUTO_INCREMENT PRIMARY KEY
or id INTEGER AUTO_INCREMENT UNIQUE KEY
.
Add JavaBean properties using the <property/>
element. The JavaBean properties in the persistence class are mapped to the columns of the database table. The name
attribute of the <property/>
element specifies the property name and is the only required attribute. The column
attribute specifies the database table column name; the default column name is the property name. The type
attribute specifies the Hibernate type. If the type
attribute is not specified, Hibernate finds the type, which might not be exactly the same as the actual type specified in the JavaBean class. To distinguish between similar Hibernate types, it is recommended that you specify the type in the property
element. Add the <property/>
elements journal
, publisher
, edition
, title
, and author
of the type string
and mapped to the columns JOURNAL
, PUBLISHER
, EDITION
, TITLE
, and AUTHOR
respectively. The catalog.hbm.xml
mapping file is listed in the following code:
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="org.jboss.hibernate.model.Catalog" table="CATALOG"> <id name="id" type="int" column="ID"> <generator class="native" /> </id> <property name="journal" column="JOURNAL" type="string" /> <property name="publisher" column="PUBLISHER" type="string" /> <property name="edition" column="EDITION" type="string" /> <property name="title" column="TITLE" type="string" /> <property name="author" column="AUTHOR" type="string" /> </class> </hibernate-mapping>
The mapping file is shown in the jboss-hibernate
project, as shown in the following screenshot: