Hi friends,
It’s all about hibernate many-to-one bidirectional mapping. Here we can discuss about country – continent relationship. That is, many countries lies in one continent. Okay, let’s start.
The DAO class, DAO.java
package hba.dao; import hba.util.HibernateUtil; import org.hibernate.FlushMode; import org.hibernate.Session; public class DAO { private static final ThreadLocal THREAD = new ThreadLocal(); protected DAO() { } public static Session getSession() { Session session = (Session) DAO.THREAD.get(); if (session == null) { session = HibernateUtil.getSessionFactory().openSession(); DAO.THREAD.set(session); getSession().setFlushMode(FlushMode.COMMIT); } return session; } protected static void begin() { getSession().beginTransaction(); } protected static void commit() { getSession().getTransaction().commit(); } protected static void rollback() { getSession().getTransaction().rollback(); getSession().close(); DAO.THREAD.set(null); } protected static void flush() { getSession().flush(); } protected static void close() { getSession().close(); DAO.THREAD.set(null); } }
The configuration file, 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.MySQL5Dialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hba</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password1.</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <mapping class="hba.pojo.m2o_b_continent"/> <mapping class="hba.pojo.m2o_b_country"/> </session-factory> </hibernate-configuration>
Here’s my country POJO, m2o_b_country.java
package hba.pojo; import java.io.Serializable; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name = "COUNTRY", catalog = "hba") public class m2o_b_country implements Serializable { private long ctry_code; private String ctry_name; private m2o_b_continent continent = new m2o_b_continent(); @Id @Column(name = "CODE", length = 20) public long getCtry_code() { return ctry_code; } @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) public m2o_b_continent getContinent() { return continent; } @Column(name = "NAME", length = 20) public String getCtry_name() { return ctry_name; } public void setContinent(m2o_b_continent continent) { this.continent = continent; } public void setCtry_code(long ctry_code) { this.ctry_code = ctry_code; } public void setCtry_name(String ctry_name) { this.ctry_name = ctry_name; } }
The continent POJO, m2o_b_continent.java
package hba.pojo; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name = "CONTINENT", catalog = "hba") public class m2o_b_continent implements Serializable { private long cnt_code; private String cnt_name; private List<m2o_b_country> countries = new ArrayList<m2o_b_country>(); @Column(name = "NAME", length = 20) public String getCnt_name() { return cnt_name; } @Id @Column(name = "CODE", length = 10) public long getCnt_code() { return cnt_code; } @OneToMany(mappedBy = "continent", cascade = CascadeType.ALL, fetch = FetchType.LAZY) public List<m2o_b_country> getCountries() { return countries; } public void setCnt_code(long cnt_code) { this.cnt_code = cnt_code; } public void setCnt_name(String cnt_name) { this.cnt_name = cnt_name; } public void setCountries(List<m2o_b_country> countries) { this.countries = countries; } public void addCountry(m2o_b_country country) { country.setContinent(this); countries.add(country); } public m2o_b_continent(long cnt_code) { this.cnt_code = cnt_code; } public m2o_b_continent() { } }
This is how we gonna persist both entities, the save() method of m2o_b_country_continent.java.
package hba.biz; import hba.dao.DAO; import hba.pojo.m2o_b_country; import hba.pojo.m2o_b_continent; import java.util.ArrayList; import java.util.List; public class m2o_b_country_continent extends DAO { m2o_b_country country1 = new m2o_b_country(); m2o_b_country country2 = new m2o_b_country(); m2o_b_continent continent = new m2o_b_continent(); List<m2o_b_country> countries = new ArrayList<m2o_b_country>(); public void save() { try { begin(); continent.setCnt_code(1); continent.setCnt_name("ASIA"); country1.setCtry_code(127); country1.setCtry_name("INDIA"); country1.setContinent(continent); country2.setCtry_code(128); country2.setCtry_name("JAPAN"); country2.setContinent(continent); countries.add(country1); countries.add(country2); continent.setCountries(countries); getSession().save(country1); getSession().save(country2); commit(); } catch (Exception e) { rollback(); System.out.println("Exception : " + e.getMessage() + "Cause : " + e.getCause()); } finally { flush(); close(); } } public static void main(String[] args) { try { m2o_b_country_continent cc = new m2o_b_country_continent(); cc.save(); } catch (Exception e) { System.out.println("Exception : " + e.getMessage() + "Cause : " + e.getCause()); } } }
The above save() method outputs.
Hibernate: select m2o_b_cont_.CODE, m2o_b_cont_.NAME as NAME12_ from hba.CONTINENT m2o_b_cont_ where m2o_b_cont_.CODE=? Hibernate: select m2o_b_coun_.CODE, m2o_b_coun_.continent_CODE as continent3_13_, m2o_b_coun_.NAME as NAME13_ from hba.COUNTRY m2o_b_coun_ where m2o_b_coun_.CODE=? Hibernate: insert into hba.CONTINENT (NAME, CODE) values (?, ?) Hibernate: insert into hba.COUNTRY (continent_CODE, NAME, CODE) values (?, ?, ?) Hibernate: insert into hba.COUNTRY (continent_CODE, NAME, CODE) values (?, ?, ?)
Here, we have created two countries and one continent. We have set both countries to the continent and set the continent of each country as the above continent object.
Now, the list of countries, listCountry() method.
package hba.biz; import hba.dao.DAO; import hba.pojo.m2o_b_country; import hba.pojo.m2o_b_continent; import java.util.ArrayList; import java.util.List; public class m2o_b_country_continent extends DAO { m2o_b_country country1 = new m2o_b_country(); m2o_b_country country2 = new m2o_b_country(); m2o_b_continent continent = new m2o_b_continent(); List<m2o_b_country> countries = new ArrayList<m2o_b_country>(); public void listCountry() { try { begin(); List<m2o_b_country> Countries = getSession().createQuery("from m2o_b_country").list(); for (m2o_b_country Country : Countries) { System.out.println("Country Code : " + Country.getCtry_code()); System.out.println("Country Name : " + Country.getCtry_name()); System.out.println("Continent Name : " + Country.getContinent().getCnt_name()); } commit(); } catch (Exception e) { rollback(); System.out.println("Exception : " + e.getMessage() + "Cause : " + e.getCause()); } finally { flush(); close(); } } public static void main(String[] args) { try { m2o_b_country_continent cc = new m2o_b_country_continent(); cc.listCountry(); } catch (Exception e) { System.out.println("Exception : " + e.getMessage() + "Cause : " + e.getCause()); } } }
It outputs.
Hibernate: select m2o_b_coun0_.CODE as CODE13_, m2o_b_coun0_.continent_CODE as continent3_13_, m2o_b_coun0_.NAME as NAME13_ from hba.COUNTRY m2o_b_coun0_ Country Code : 127 Country Name : INDIA Hibernate: select m2o_b_cont0_.CODE as CODE12_0_, m2o_b_cont0_.NAME as NAME12_0_ from hba.CONTINENT m2o_b_cont0_ where m2o_b_cont0_.CODE=? Continent Name : ASIA Country Code : 128 Country Name : JAPAN Continent Name : ASIA
Now, the list of continents with its countries, listContinent() method.
package hba.biz; import hba.dao.DAO; import hba.pojo.m2o_b_country; import hba.pojo.m2o_b_continent; import java.util.ArrayList; import java.util.List; public class m2o_b_country_continent extends DAO { m2o_b_country country1 = new m2o_b_country(); m2o_b_country country2 = new m2o_b_country(); m2o_b_continent continent = new m2o_b_continent(); List<m2o_b_country> countries = new ArrayList<m2o_b_country>(); public void listContinent() { try { begin(); List<m2o_b_continent> Continents = getSession().createQuery("from m2o_b_continent").list(); for (m2o_b_continent Continent : Continents) { System.out.println("Continent Name : " + Continent.getCnt_name()); List<m2o_b_country> Countries = Continent.getCountries(); for (m2o_b_country Country : Countries) { System.out.println("Country Code : " + Country.getCtry_code()); System.out.println("Country Name : " + Country.getCtry_name()); } } commit(); } catch (Exception e) { rollback(); System.out.println("Exception : " + e.getMessage() + "Cause : " + e.getCause()); } finally { flush(); close(); } } public static void main(String[] args) { try { m2o_b_country_continent cc = new m2o_b_country_continent(); cc.listContinent(); } catch (Exception e) { System.out.println("Exception : " + e.getMessage() + "Cause : " + e.getCause()); } } }
And, the output will be…
Hibernate: select m2o_b_cont0_.CODE as CODE12_, m2o_b_cont0_.NAME as NAME12_ from hba.CONTINENT m2o_b_cont0_ where m2o_b_cont0_.CODE=1252 Continent Name : ASIA Hibernate: select countries0_.continent_CODE as continent3_1_, countries0_.CODE as CODE1_, countries0_.CODE as CODE13_0_, countries0_.continent_CODE as continent3_13_0_, countries0_.NAME as NAME13_0_ from hba.COUNTRY countries0_ where countries0_.continent_CODE=? Country Code : 127 Country Name : INDIA Country Code : 128 Country Name : JAPAN
Thanks.