How to store Continents having set of country objects ?
-------------------------------------------------------------------------
I am going to take example of Continents and Countries to explain example on One-to Many relationship.
As every one knows every Continent can have more than one countries.
Steps to store Continents and its Countries
---------------------------------------------------------
1)First create ContinentTo.Create continId and contiName.These properties are not new.
The new property we need is a countries property which is Set.When you are working
with hibernate.. you have to choose interface type(Set) in the left side as in the following
declaration.There is the reason to do that.. we will see in next posts to keep the example
simple.
private Set countries = new HashSet();
The countries set is going to hold countries for a Continent.
2)Create getter and setters for above 3 properties.
3)Create the following continents.hbm.xml file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="javalearners.ContinentTo" table="dbcontinents">
<id name="contiId" column="dbcontiId">
<generator class="increment"/>
</id>
<property name="contiName" column="dbContiName"/>
<set name="countries" inverse="true">
<key column="contiFk" ></key>
<one-to-many class="javalearners.CountryTo"/>
</set>
</class>
</hibernate-mapping>
There is nothing new in the above hbm except set element.
4)We will discuss the <set element in the above hbm file which is used to map countries in to a
seperate table.
5) <set name="countries" inverse="true">
<key column="contiFk" ></key>
<one-to-many class="javalearners.CountryTo"/>
</set>
6)In the above element name attribute value matches the set variable declared in our ContinentTo.java
7)For one to many relationship in the database the most important thing is the foreign key right ?
It is the foreign key in the countries table that links continents and countries tables together.
8)So, we need to specify the foreign key column using <key element. Specify the foreign key
column which is going to hold.. the foriegn key.In my example I have given column contiFk..
that means if I store Continent Asia with 2 countries India and China.
9)In database. if Contient Asia is stored with primary key 1 in continents table,then in countries
table 2 countries will be stored and in each record 1 is stored in contiFk column in countries
table. That means we can understand that these countries belong to Continent 1 which is
Asia.
10)We have to specify the class to which Continent is having one to many relationship using
<one-to-many element>.Just specify the class in class atrribute's value.
11)Create countriesTo and Countries.hbm.xml.Here is countries.hbm.xml.
There is nothing new in this xml.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="javalearners.CountryTo" table="dbcountries">
<id name="countryId" column="dbcountryid">
<generator class="increment"/>
</id>
<property name="countryName" column="dbcountryname"/>
</class>
</hibernate-mapping>
12)Declare both mapping files in hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.hbm2ddl.auto" >create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="student.hbm.xml"/>
<mapping resource="continents.hbm.xml"/>
<mapping resource="countries.hbm.xml"/>
</session-factory>
</hibernate-configuration>
13)I have added new property called hibernate.show_sql and set it to true.
What it does is,hibernate will show the sql that it is generating to the console.
Following are the lines that got printed on my netbeans console after I set to
true.This is very useful to find out where we are doing wrong.
INFO: schema export complete
Hibernate: select max(dbcontiId) from dbcontinents
Hibernate: select max(dbcountryid) from dbcountries
Hibernate: insert into dbcontinents (dbContiName, dbcontiId) values (?, ?)
Hibernate: insert into dbcountries (dbcountryname, dbcountryid) values (?, ?)
Hibernate: insert into dbcountries (dbcountryname, dbcountryid) values (?, ?)
Hibernate: update dbcountries set contiFk=? where dbcountryid=?
Hibernate: update dbcountries set contiFk=? where dbcountryid=?
14)Uploaded the complete example to the following URL.
https://docs.google.com/file/d/0BwZaaDwCofcNMGhuNlBSYlNOZ1E/edit?usp=sharing
Hope you understand how to map.. objects with one to many relationship..
View of the Continents and Countries tables that got created by hibernate
automatically.You can see the CONTIFK in the countries table.1 means
it belong to Continent ASIA.

-------------------------------------------------------------------------
I am going to take example of Continents and Countries to explain example on One-to Many relationship.
As every one knows every Continent can have more than one countries.
Steps to store Continents and its Countries
---------------------------------------------------------
1)First create ContinentTo.Create continId and contiName.These properties are not new.
The new property we need is a countries property which is Set.When you are working
with hibernate.. you have to choose interface type(Set) in the left side as in the following
declaration.There is the reason to do that.. we will see in next posts to keep the example
simple.
private Set countries = new HashSet();
The countries set is going to hold countries for a Continent.
2)Create getter and setters for above 3 properties.
3)Create the following continents.hbm.xml file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="javalearners.ContinentTo" table="dbcontinents">
<id name="contiId" column="dbcontiId">
<generator class="increment"/>
</id>
<property name="contiName" column="dbContiName"/>
<set name="countries" inverse="true">
<key column="contiFk" ></key>
<one-to-many class="javalearners.CountryTo"/>
</set>
</class>
</hibernate-mapping>
There is nothing new in the above hbm except set element.
4)We will discuss the <set element in the above hbm file which is used to map countries in to a
seperate table.
5) <set name="countries" inverse="true">
<key column="contiFk" ></key>
<one-to-many class="javalearners.CountryTo"/>
</set>
6)In the above element name attribute value matches the set variable declared in our ContinentTo.java
7)For one to many relationship in the database the most important thing is the foreign key right ?
It is the foreign key in the countries table that links continents and countries tables together.
8)So, we need to specify the foreign key column using <key element. Specify the foreign key
column which is going to hold.. the foriegn key.In my example I have given column contiFk..
that means if I store Continent Asia with 2 countries India and China.
9)In database. if Contient Asia is stored with primary key 1 in continents table,then in countries
table 2 countries will be stored and in each record 1 is stored in contiFk column in countries
table. That means we can understand that these countries belong to Continent 1 which is
Asia.
10)We have to specify the class to which Continent is having one to many relationship using
<one-to-many element>.Just specify the class in class atrribute's value.
11)Create countriesTo and Countries.hbm.xml.Here is countries.hbm.xml.
There is nothing new in this xml.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="javalearners.CountryTo" table="dbcountries">
<id name="countryId" column="dbcountryid">
<generator class="increment"/>
</id>
<property name="countryName" column="dbcountryname"/>
</class>
</hibernate-mapping>
12)Declare both mapping files in hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:XE</property>
<property name="hibernate.connection.username">hr</property>
<property name="hibernate.connection.password">hr</property>
<property name="hibernate.hbm2ddl.auto" >create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="student.hbm.xml"/>
<mapping resource="continents.hbm.xml"/>
<mapping resource="countries.hbm.xml"/>
</session-factory>
</hibernate-configuration>
13)I have added new property called hibernate.show_sql and set it to true.
What it does is,hibernate will show the sql that it is generating to the console.
Following are the lines that got printed on my netbeans console after I set to
true.This is very useful to find out where we are doing wrong.
INFO: schema export complete
Hibernate: select max(dbcontiId) from dbcontinents
Hibernate: select max(dbcountryid) from dbcountries
Hibernate: insert into dbcontinents (dbContiName, dbcontiId) values (?, ?)
Hibernate: insert into dbcountries (dbcountryname, dbcountryid) values (?, ?)
Hibernate: insert into dbcountries (dbcountryname, dbcountryid) values (?, ?)
Hibernate: update dbcountries set contiFk=? where dbcountryid=?
Hibernate: update dbcountries set contiFk=? where dbcountryid=?
14)Uploaded the complete example to the following URL.
https://docs.google.com/file/d/0BwZaaDwCofcNMGhuNlBSYlNOZ1E/edit?usp=sharing
Hope you understand how to map.. objects with one to many relationship..
View of the Continents and Countries tables that got created by hibernate
automatically.You can see the CONTIFK in the countries table.1 means
it belong to Continent ASIA.
No comments:
Post a Comment