[an error occurred while processing this directive]

Hibernate Relationships - Settingup database

                         

Hibernate Relationships - Setting up database:

In this section we will setup the database for the tutorial. We are using MySQL database for this tutorial. You can download MySQL and install on your computer. More information on installing MySQL is available at http://www.roseindia.net/mysql/mysql5/Installing-MySQL-on-Windows.shtml.

You can use command line tool or any MySQL GUI to connect view the tables, structure and data in the MySQL database. Here is small tutorial on using MySQL client http://www.roseindia.net/mysql/mysql.shtml.

Now let's setup the database and create the MySQL schema. We will use the Hibernate hbm2ddl utility to create/update the table structure at run time. Here is the property defined into hibernate.cfg.xml  file:

  <property name="hibernate.hbm2ddl.auto">update</property>

The above utility is good and can be used at development time, in production application you must set the value to none. Use this utility carefully otherwise it will drop your existing tables along with the data.

Step1:

Create MySQL database using the following command:

create database hibrel;

In this example we will use the database called hibrel to run the examples.

  1. Hibernate service
  2. Persistence class configuration

Step 2:

Download the source code of examples discusses in the tutorial by clicking here HibRelationships.zip. Open the Eclipse IDE and create a new project from the downloaded source. We have included all the jar files required to run the example. 

Step 3:

Open hibernate.cfg.xml file and change the database connection url, password and user name to connect to database.

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibrel</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

While configuring the Hiberbate Service we provide database connection url and passwords. We also provide different caching level for the application performance.

Here is the code of hibernate.cfg.xml file used in our example application

<?xml version='1.0' encoding='utf-8'?>

<!DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibrel</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.password">root</property>

<property name="hibernate.connection.pool_size">10</property>

<property name="show_sql">true</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping files -->

<mapping resource="Employee.hbm.xml"/>

<mapping resource="Group.hbm.xml"/>

<mapping resource="author.hbm.xml"/>

<mapping resource="book.hbm.xml"/>

</session-factory>

</hibernate-configuration>

Step 3:

To create the database schema, open OneToManyRelation.java in the Eclipse and right click and select Run As--> Java application. Hibernate will create required tables for the tutorial.

The Eclipse should display the following output:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: insert into gropuptable (name) values (?)

Hibernate: select max(id) from story

Group id:1

Hibernate: insert into story (info, parent_id, id) values (?, ?, ?)

Hibernate: insert into story (info, parent_id, id) values (?, ?, ?)

Hibernate: insert into story (info, parent_id, id) values (?, ?, ?)

Hibernate: insert into story (info, parent_id, id) values (?, ?, ?)

Hibernate: update story set parent_id=?, idx=? where id=?

Hibernate: update story set parent_id=?, idx=? where id=?

Hibernate: update story set parent_id=?, idx=? where id=?

Hibernate: update story set parent_id=?, idx=? where id=?

Now check your database and following tables should present:

  1. authorauthor_book
  2. book
  3. employeeemployee_address
  4. gropuptable
  5. story

Usually <persistence-object>.hbm.xml file is used to configure the Persistence object. Here is the example employee.hbm.xml configuration used in our application.

Here is the script to create database manually:

CREATE TABLE `author` (
`id` int(11) NOT NULL,
`authorName` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`bookName` varchar(250) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `author_book` (
`authorId` int(11) NOT NULL,
`bookId` int(11) NOT NULL,
PRIMARY KEY (`bookId`,`authorId`),
KEY `FK2A7A111DB63F85B3` (`bookId`),
KEY `FK2A7A111DFC08F7` (`authorId`)
) ENGINE=MyISAM

CREATE TABLE `employee` (
`employee_id` int(11) NOT NULL AUTO_INCREMENT,
`employee_name` varchar(50) NOT NULL,
PRIMARY KEY (`employee_id`)
) ENGINE=MyISAM;

 CREATE TABLE `employee_address` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`address` varchar(255) NOT NULL,
`country` varchar(100) NOT NULL,
`emp_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `gropuptable` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM;

CREATE TABLE `story` (
`id` int(11) NOT NULL,
`info` varchar(255) DEFAULT NULL,
`parent_id` int(11) DEFAULT NULL,
`idx` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK68AF8F58984AF09` (`parent_id`)
) ENGINE=MyISAM;

 

In next section we will learn how to create and run the one-to-one mapping in Hibernate.

                         

[an error occurred while processing this directive]