Generating a Database Table from an XML Mapping File
In this section, you'll see how to generate a database table from a XML mapping file. You can do this using a hibernate.properties file, which specifies the database configuration parameters, and an .hbm.xml mapping file. If you use a configuration file (hibernate.cfg.xml) to specify the database properties, you won't need the hibernate.properties file. The .hbm.xml mapping file contains class definitions which specify the XML document nodes to be mapped to a database table, the database table name, and the table columns corresponding to the XML document. It also specifies these attributes: column type, length, not-null, and unique. Some of the mapping file elements are listed in Table 2.
|
Element
|
Description
|
|
hibernate-mapping
|
The root element.
Attributes: schema
and package.
Sub Elements: class
|
|
class
|
Specifies the
entity to map to a database table.
Attributes: table,
schema, entity-name, node.
Sub Elements: id,
property, set, list.
|
|
id
|
Required element in
a class definition.
Attributes: column,
type, length
Sub Elements:
column, generator
|
|
property
|
Specifies a entity
property and the corresponding database table column.
Attributes: type,
column, length, not-null.
Sub Elements:
column
|
Table 2. Hibernate Mapping File Elements
The example mapping file in this tutorial specifies the entity Catalog, which consists of nodes/columns for a journal. The XML document nodes are specified in node attribute. The example mapping file, Catalog.hbm.xml is listed in Listing 1. The <generator class="native"/> element specifies the identifier generation strategy.
You can specify the JDBC configuration properties in the Hibernate properties file (hibernate.properties). The hibernate.properties text file contains properties specified with <property>=<value>. Some of the JDBC configuration properties are listed in Table 3.
|
Property
|
Description
|
|
hibernate.connection.driver_class
|
Driver class to connect with the
database.
|
|
hibernate.connection.url
|
Connection URL to connect to the database.
|
|
hibernate.dialect
|
The Dialect for the database.
|
|
hibernate.connection.username
|
Username to login to the database.
|
|
hibernate.connection.password
|
Password to login to the database.
|
|
hibernate.default_schema
|
Database schema in which a database
table gets generated if a schema name is not specified in the hbm.xml mapping file.
|
Table 3. Hibernate Properties
This tutorial's hibernate.properties file specifies the driver class as oracle.jdbc.driver.OracleDriver, the schema is OE, the connection URL is for the Oracle thin type 4 driver, and the dialect is for the Oracle database.
This is what the hibernate.properties file for the Oracle databaselooks like:
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:OracleDB
hibernate.connection.username=OE
hibernate.connection.password=calgary
hibernate.dialect=org.hibernate.dialect.OracleDialect
In this section, you will generate an Oracle database table from the Hibernate mapping file (Catalog.hbm.xml) and the properties file (hibernate.properties). The Hibernate toolset provides the org.hibernate.tool.hbm2ddl.SchemaExport tool to generate a database table from a mapping file.
The mapping file specifies the database table name. Copy the hibernate.properties and Catalog.hbm.xml files to the same directory, c:/Hibernate XML, for example. Start the Oracle database. Next, generate the database table with SchemaExport tool:
C:\>Hibernate>java org.hibernate.tool.hbm2ddl.SchemaExport
--properties=hibernate.properties Catalog.hbm.xml
This creates a database table. Here's the output from the SchemaExport tool:
drop table OE.CATALOG cascade constraints
drop sequence hibernate_sequence
create table OE.CATALOG (CatalogId varchar2(255) not null, JOURNAL varchar2(255), PUBLISHER
varchar2(255), EDITION varchar2(255), TITLE varchar2(255), AUTHOR varchar2(255), primary key
(CatalogId))
create sequence hibernate_sequence
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.
|