Friday, 25 March 2016

H03 : Association Mapping using Hibernate XML - Bidirectional


Steps :
1. Create a Java Project
2. Write hibernate configuration file and Hiberante util file
3. Write the domain objects
4. Write mapping documents
5. Write Insert and Select client

You can see the project as :
Files which you have to write :
1. hibernate.cfg.xml
2. HibernateUtil.java
3. Student.java
4. student.hbm.xml
5. Nick.java
6. Address.java
7. School.java
8. Bike.java
9. Game.java
10. Account.java
11. H03.java

Codes :
1. hibernate.cfg.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration SYSTEM   
 "file:\\\D:\lib\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/practice</property>  
         <property name="hibernate.connection.username">root</property>  
         <property name="hibernate.connection.password">root</property>  
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
         <property name="hibernate.show_sql">false</property>  
         <property name="hbm2ddl.auto">create</property>  
         <mapping resource="com/hibernate/association/student.hbm.xml" />  
     </session-factory>  
 </hibernate-configuration> 
2. HibernateUtil.java
 package com.hibernate.util;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.cfg.Configuration;  
 public class HibernateUtil {  
     static SessionFactory factory = null;  
     static {  
         try {  
             Configuration cfg = new Configuration();  
             cfg = cfg.configure();  
             factory = cfg.buildSessionFactory();  
         } catch (Exception e) {  
             System.out.println("Error in Initializing SessionFactory");  
             e.printStackTrace();  
         }  
     }  
     public static SessionFactory getSessionFactory() {  
         return factory;  
     }  
 } 
3. Student.java
 package com.hibernate.association;  
 import java.util.Set;  
 /**  
  * Domain class Student  
  *   
  * @author bkar  
  *  
  */  
 public class Student {  
     private int sid;  
     private String sname;  
     // One To One - Primary Key - Bidirectional  
     private Nick nick;  
     // One To One - Foreign Key - Bidirectional  
     private Address address;  
     // Many To One/One To Many - Bidirectional  
     private School school;  
     // One To One - Join Table - Bidirectional  
     private Bike bike;  
     // Many To One/One To Many - Bidirectional  
     private Game game;  
     // Many To Many - Bidirectional  
     private Set<Account> accounts;   
     public Student() {  
         super();  
     }  
     public Student(String sname) {  
         super();  
         this.sname = sname;  
     }  
     public int getSid() {  
         return sid;  
     }  
     public void setSid(int sid) {  
         this.sid = sid;  
     }  
     public String getSname() {  
         return sname;  
     }  
     public void setSname(String sname) {  
         this.sname = sname;  
     }  
     public Nick getNick() {  
         return nick;  
     }  
     public void setNick(Nick nick) {  
         this.nick = nick;  
     }  
     public Address getAddress() {  
         return address;  
     }  
     public void setAddress(Address address) {  
         this.address = address;  
     }  
     public School getSchool() {  
         return school;  
     }  
     public void setSchool(School school) {  
         this.school = school;  
     }  
     public Bike getBike() {  
         return bike;  
     }  
     public void setBike(Bike bike) {  
         this.bike = bike;  
     }  
     public Game getGame() {  
         return game;  
     }  
     public void setGame(Game game) {  
         this.game = game;  
     }  
     public Set<Account> getAccounts() {  
         return accounts;  
     }  
     public void setAccounts(Set<Account> accounts) {  
         this.accounts = accounts;  
     }  
 } 
4. student.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="Student" table="student" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <!-- One To One - Primary Key - Bidirectional -->  
         <one-to-one name="nick"/>  
         <!-- One To One - Foreign Key - Bidirectional -->  
         <many-to-one name="address" class="Address" column="adid" unique="true" />  
         <!-- Many To One/One To Many - Bidirectional -->  
         <many-to-one name="school" class="School" column="schid" />  
         <!-- Many To Many - Join Table - Bidirectional -->  
         <!-- Should be above join tags -->  
        <set name="accounts" table="student_account">  
            <key column="sid"/>  
            <many-to-many column="acctId" class="Account"/>  
        </set>  
        <!-- One To One - Join Table - Bidirectional -->  
         <join table="student_bike">  
               <key column="sid" unique="true" />  
               <many-to-one name="bike" column="bikeId" unique="true" />  
          </join>  
          <!-- Many To One/One To Many - Join Table - Bidirectional -->  
          <join table="student_game" optional="true">  
            <key column="sid"/>  
            <many-to-one name="game" column="gameId"/>  
        </join>  
     </class>  
     <class name="Nick" table="nick" lazy="false">  
         <id name="nid" column="nid" type="int">  
      <generator class="increment" />  
         </id>  
         <property name="nickName" column="nickName" type="string" />  
         <!-- One To One - Primary Key - Bidirectional -->  
         <one-to-one name="student"/>  
     </class>  
     <class name="Address" table="address" lazy="false">  
         <id name="adid" column="adid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="street" column="street" type="string" />  
         <property name="city" column="city" type="string" />  
         <property name="state" column="state" type="string" />  
         <!-- One To One - Foreign Key - Bidirectional -->  
         <one-to-one name="student" class="Student" property-ref="address"/>  
     </class>  
     <class name="School" table="school" lazy="false">  
         <id name="schid" column="schid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="name" column="name" type="string" />  
         <property name="estbYear" column="estbYear" type="string" />  
         <!-- Many To One/One To Many - Bidirectional -->  
         <set name="students">  
             <key column="schid"/>  
             <one-to-many class="Student"/>  
         </set>  
     </class>  
     <class name="Bike" table="bike" lazy="false">  
         <id name="bikeId" column="bikeId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="bikeNo" column="bikeNo" type="string" />  
         <property name="bikeBrand" column="bikeBrand" type="string" />  
         <property name="bikeMfd" column="bikeMfd" type="string"/>  
         <!-- One To One - Join Table - Bidirectional -->  
          <join table="student_bike" optional="true" inverse="true">  
             <key column="bikeId" unique="true"/>  
             <many-to-one name="student" column="sid" unique="true"/>  
         </join>   
     </class>  
     <class name="Game" table="game" lazy="false">  
         <id name="gameId" column="gameId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="gameName" column="gameName" type="string" />  
         <property name="gamePosition" column="gamePosition" type="string" />  
         <!-- Many To One/One To Many - Join Table - Bidirectional -->  
         <set name="students" table="student_game" inverse="true">  
              <key column="gameId"/>  
              <many-to-many column="sid" class="Student"/>  
          </set>  
     </class>  
     <class name="Account" table="account" lazy="false">  
         <id name="acctId" column="acctId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="acctType" column="acctType" type="string" />  
         <property name="contacts" column="contacts" type="int" />  
         <!-- Many To Many - Bidirectional -->  
         <set name="students" table="student_account">  
             <key column="acctId"/>  
             <many-to-many column="sid" class="Student"/>  
         </set>  
     </class>  
 </hibernate-mapping> 
5. Nick.java
 package com.hibernate.association;  
 /**  
  * Domain class attached with Student - One to One - Primary Key - Bidirectional  
  * @author bkar  
  *  
  */  
 public class Nick {  
     private int nid;  
     private String nickName;  
     // One To One - Primary Key - Bidirectional  
     private Student student;  
     public Nick() {  
         super();  
     }  
     public Nick(String nickName) {  
         super();  
         this.nickName = nickName;  
     }  
     public int getNid() {  
         return nid;  
     }  
     public void setNid(int nid) {  
         this.nid = nid;  
     }  
     public String getNickName() {  
         return nickName;  
     }  
     public void setNickName(String nickName) {  
         this.nickName = nickName;  
     }  
     public Student getStudent() {  
         return student;  
     }  
     public void setStudent(Student student) {  
         this.student = student;  
     }  
 } 
6. Address.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - One to One - Foreign Key - Bidirectional  
  * @author bkar  
  *  
  */  
 public class Address {  
     private int adid;  
     private String street;  
     private String city;  
     private String state;  
     // One To One - Foreign Key - Bidirectional  
     private Student student;  
     public Address() {  
         super();  
     }  
     public Address(String street, String city, String state) {  
         super();  
         this.street = street;  
         this.city = city;  
         this.state = state;  
     }  
     public int getAdid() {  
         return adid;  
     }  
     public void setAdid(int adid) {  
         this.adid = adid;  
     }  
     public String getStreet() {  
         return street;  
     }  
     public void setStreet(String street) {  
         this.street = street;  
     }  
     public String getCity() {  
         return city;  
     }  
     public void setCity(String city) {  
         this.city = city;  
     }  
     public String getState() {  
         return state;  
     }  
     public void setState(String state) {  
         this.state = state;  
     }  
     public Student getStudent() {  
         return student;  
     }  
     public void setStudent(Student student) {  
         this.student = student;  
     }  
 } 
7. School.java
 package com.hibernate.association;  
 import java.util.Set;  
 /**  
  * Doamin class attached to Student - One to Many/Many to One - Bidirectional  
  * @author bkar  
  *  
  */  
 public class School {  
     private int schid;  
     private String name;  
     private String estbYear;  
     private Set<Student> students;  
     public School() {  
         super();  
     }  
     public School(String name, String estbYear) {  
         super();  
         this.name = name;  
         this.estbYear = estbYear;  
     }  
     public int getSchid() {  
         return schid;  
     }  
     public void setSchid(int schid) {  
         this.schid = schid;  
     }  
     public String getName() {  
         return name;  
     }  
     public void setName(String name) {  
         this.name = name;  
     }  
     public String getEstbYear() {  
         return estbYear;  
     }  
     public void setEstbYear(String estbYear) {  
         this.estbYear = estbYear;  
     }  
     public Set<Student> getStudents() {  
         return students;  
     }  
     public void setStudents(Set<Student> students) {  
         this.students = students;  
     }  
 } 
8. Bike.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - One to One - Join Table - Bidirectional  
  * @author bkar  
  *  
  */  
 public class Bike {  
     private int bikeId;  
     private String bikeNo;  
     private String bikeBrand;  
     private String bikeMfd;  
     // One To One - Join Table - Bidirectional  
     private Student student;  
     public Bike() {  
         super();  
     }  
     public Bike(String bikeNo, String bikeBrand, String bikeMfd) {  
         super();  
         this.bikeNo = bikeNo;  
         this.bikeBrand = bikeBrand;  
         this.bikeMfd = bikeMfd;  
     }  
     public int getBikeId() {  
         return bikeId;  
     }  
     public void setBikeId(int bikeId) {  
         this.bikeId = bikeId;  
     }  
     public String getBikeNo() {  
         return bikeNo;  
     }  
     public void setBikeNo(String bikeNo) {  
         this.bikeNo = bikeNo;  
     }  
     public String getBikeBrand() {  
         return bikeBrand;  
     }  
     public void setBikeBrand(String bikeBrand) {  
         this.bikeBrand = bikeBrand;  
     }  
     public String getBikeMfd() {  
         return bikeMfd;  
     }  
     public void setBikeMfd(String bikeMfd) {  
         this.bikeMfd = bikeMfd;  
     }  
     public Student getStudent() {  
         return student;  
     }  
     public void setStudent(Student student) {  
         this.student = student;  
     }  
 } 
9. Game.java
 package com.hibernate.association;  
 import java.util.Set;  
 /**  
  * Domain class attached to Student - Many to One/One to Many - Join table - Bidirectional  
  * @author bkar  
  *  
  */  
 public class Game {  
     private int gameId;  
     private String gameName;  
     private String gamePosition;  
     // One To Many/Many To One - Join Table - Bidirectional   
     private Set<Student> students;  
     public Game() {  
         super();  
     }  
     public Game(String gameName, String gamePosition) {  
         super();  
         this.gameName = gameName;  
         this.gamePosition = gamePosition;  
     }  
     public int getGameId() {  
         return gameId;  
     }  
     public void setGameId(int gameId) {  
         this.gameId = gameId;  
     }  
     public String getGameName() {  
         return gameName;  
     }  
     public void setGameName(String gameName) {  
         this.gameName = gameName;  
     }  
     public String getGamePosition() {  
         return gamePosition;  
     }  
     public void setGamePosition(String gamePosition) {  
         this.gamePosition = gamePosition;  
     }  
     public Set<Student> getStudents() {  
         return students;  
     }  
     public void setStudents(Set<Student> students) {  
         this.students = students;  
     }  
 } 
10. Account.java
 package com.hibernate.association;  
 import java.util.Set;  
 /**  
  * Domain class attached to Student - Many to Many - Join Table - Bidirectional   
  * @author bkar  
  *  
  */  
 public class Account {  
     private int acctId;  
     private String acctType;  
     private int contacts;  
     // Many To Many - Bidirectional  
     private Set<Student> students;  
     public Account() {  
         super();  
     }  
     public Account(String acctType, int contacts) {  
         super();  
         this.acctType = acctType;  
         this.contacts = contacts;  
     }  
     public int getAcctId() {  
         return acctId;  
     }  
     public void setAcctId(int acctId) {  
         this.acctId = acctId;  
     }  
     public String getAcctType() {  
         return acctType;  
     }  
     public void setAcctType(String acctType) {  
         this.acctType = acctType;  
     }  
     public int getContacts() {  
         return contacts;  
     }  
     public void setContacts(int contacts) {  
         this.contacts = contacts;  
     }  
     @Override  
     public String toString() {  
         return "Account("+acctId+")";  
     }  
     public Set<Student> getStudents() {  
         return students;  
     }  
     public void setStudents(Set<Student> students) {  
         this.students = students;  
     }  
 } 
11. H03.java
 package com.client;  
 import java.util.HashSet;  
 import java.util.List;  
 import java.util.Set;  
 import org.hibernate.Query;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import com.hibernate.association.Account;  
 import com.hibernate.association.Address;  
 import com.hibernate.association.Bike;  
 import com.hibernate.association.Game;  
 import com.hibernate.association.Nick;  
 import com.hibernate.association.School;  
 import com.hibernate.association.Student;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  * Insert and Select Client for   
  * One To One - Primary Key - Bidirectional   
  * One To One - Foreign Key - Bidirectional   
  * One To Many/Many To One - Bidirectional   
  * One To One - Join Table - Bidirectional   
  * One To Many/Many To One - Join Table -Bidirectional   
  * Many To Many - Join Table - Bidirectional  
  * @author bkar  
  */  
 public class H03 {  
     public static void main(String[] args) {  
         SessionFactory sf = null;  
         Session s = null;  
         Transaction tx = null;  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             s = sf.openSession();  
             tx = s.beginTransaction();  
             // Create Domain Objects  
             Student stu1 = new Student("Ram");  
             Student stu2 = new Student("Shyam");  
             Nick nick1 = new Nick("petV");  
             Nick nick2 = new Nick("petA");  
             Address address1 = new Address("street1", "city1", "state1");  
             Address address2 = new Address("street2", "city2", "state2");  
             School school1 = new School("ABCD", "2001");  
             Bike bike1 = new Bike("1111", "Yamaha", "2001");  
             Bike bike2 = new Bike("2222", "Suzuki", "2002");  
             Game game1 = new Game("Cricket", "Bowler");  
             Account account1 = new Account("Facebook", 124);  
             Account account2 = new Account("Yahoo", 111);  
             // Assigning the relationship from both sides  
             stu1.setNick(nick1);     // One Student To One Nick - One to One Bidirectional - Primary Key  
             stu2.setNick(nick2);     // One Student To One Nick - One to One Bidirectional - Primary Key  
             nick1.setStudent(stu1);    // One Nick To One Student - One to One Bidirectional - Primary Key  
             nick2.setStudent(stu2);    // One Nick To One Student - One to One Bidirectional - Primary Key  
             stu1.setAddress(address1); // One Student To One Address - One to One Bidirectional - Foreign Key  
             stu2.setAddress(address2); // One Student To One Address - One to One Bidirectional - Foreign Key  
             address1.setStudent(stu1); // One Address To One Student - One to One Bidirectional - Foreign Key  
             address2.setStudent(stu2); // One Address To One Student - One to One Bidirectional - Foreign Key  
             stu1.setSchool(school1); // Many Student To One School - Many To One/One To Many Bidirectional  
             stu2.setSchool(school1); // Many Student To One School - Many To One/One To Many Bidirectional  
             Set<Student> setofstudents = new HashSet<Student>();  
             setofstudents.add(stu1);  
             setofstudents.add(stu2);  
             school1.setStudents(setofstudents); // One School To Many Student - Many To One/One To Many Bidirectional  
             stu1.setBike(bike1);     // One Student To One Bike - One to One Bidirectional Join Table   
             stu2.setBike(bike2);     // One Student To One Bike - One to One Bidirectional Join Table  
             bike1.setStudent(stu1); // One Bike To One Student - One to One Bidirectional Join Table   
             bike2.setStudent(stu2); // One Bike To One Student - One to One Bidirectional Join Table   
             stu1.setGame(game1); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table  
             stu2.setGame(game1); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table  
             game1.setStudents(setofstudents); // One Game To Many Student - Many To One/One To Many Bidirectional Join Table  
             Set<Account> setofaccounts = new HashSet<Account>();  
             setofaccounts.add(account1);  
             setofaccounts.add(account2);  
             stu1.setAccounts(setofaccounts);     // Many Student To Many Account - Many To Many Bidirectional Join Table  
             stu2.setAccounts(setofaccounts);     // Many Student To Many Account - Many To Many Bidirectional Join Table  
 //            account1.setStudents(setofstudents);// Many Account To Many Student - Many To Many Bidirectional Join Table  
 //            account2.setStudents(setofstudents);// Many Account To Many Student - Many To Many Bidirectional Join Table  
             // Persist the domain objects   
             s.save(nick1);  
             s.save(nick2);  
             s.save(address1);  
             s.save(address2);  
             s.save(school1);  
             s.save(bike1);  
             s.save(bike2);  
             s.save(account1);  
             s.save(account2);  
             s.save(game1);  
             s.save(stu1);  
             s.save(stu2);  
             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
             // Assigning the relationship in reverse direction only would be enough  
             Student stu3 = new Student("Gopal");  
             Student stu4 = new Student("Rahim");  
             Nick nick3 = new Nick("petB");  
             Nick nick4 = new Nick("petC");  
             nick3.setStudent(stu3); // One Nick To One Student - One to One Bidirectional - Primary Key  
             nick4.setStudent(stu4); // One Nick To One Student - One to One Bidirectional - Primary Key  
             Address address3 = new Address("street3", "city3", "state3");  
             Address address4 = new Address("street4", "city4", "state4");  
             stu3.setAddress(address3); // One Student To One Address - One to One Bidirectional - Foreign Key  
             stu4.setAddress(address4); // One Student To One Address - One to One Bidirectional - Foreign Key  
             address3.setStudent(stu3); // One Address To One Student - One to One Bidirectional - Foreign Key  
             address4.setStudent(stu4); // One Address To One Student - One to One Bidirectional - Foreign Key  
             School school2 = new School("EFGH","2002");  
             Set<Student> setofstudents2 = new HashSet<Student>();  
             setofstudents2.add(stu3);  
             setofstudents2.add(stu4);  
             school2.setStudents(setofstudents2); // One School To Many Student - Many To One/One To Many Bidirectional  
             Bike bike3 = new Bike("3333", "Kawasaki", "2011");  
             Bike bike4 = new Bike("4444", "Honda", "2012");  
             stu3.setBike(bike3);     // One Student To One Bike - One to One Bidirectional Join Table   
             stu4.setBike(bike4);     // One Student To One Bike - One to One Bidirectional Join Table  
             bike3.setStudent(stu3); // One Bike To One Student - One to One Bidirectional Join Table   
             bike4.setStudent(stu4); // One Bike To One Student - One to One Bidirectional Join Table   
             Game game2 = new Game("Football", "Center");  
 //            game2.setStudents(setofstudents); // Not working - Not able to save the relationship into Join table  
             stu3.setGame(game2); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table  
             stu4.setGame(game2); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table  
             Account account3 = new Account("MySpace", 99);  
             Account account4 = new Account("Orkut", 234);  
             Set<Student> setofstu1 = new HashSet<Student>();  
             setofstu1.add(stu3);  
             setofstu1.add(stu4);  
             account3.setStudents(setofstu1); // Many Account To Many Student - Many To Many Bidirectional Join Table  
             account4.setStudents(setofstu1); // Many Account To Many Student - Many To Many Bidirectional Join Table  
             s.save(nick3);  
             s.save(nick4);  
             s.save(address3);  
             s.save(address4);  
             s.save(school2);  
             s.save(bike3);  
             s.save(bike4);  
             s.save(game2);  
             s.save(account3);  
             s.save(account4);  
             s.save(stu3);  
             s.save(stu4);  
             // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
             // Assigning the relationship in forward direction only would be enough  
             // Create Domain Objects  
             Student stu5 = new Student("Nilesh");  
             Student stu6 = new Student("Neeraj");  
             Nick nick5 = new Nick("Nilu");  
             Nick nick6 = new Nick("Neru");  
             Address address5 = new Address("street5", "city5", "state5");  
             Address address6 = new Address("street6", "city6", "state6");  
             School school3 = new School("IJKL", "2003");  
             Bike bike5 = new Bike("5555", "Yamaha", "2012");  
             Bike bike6 = new Bike("6666", "Suzuki", "2011");  
             Game game3 = new Game("Hockey", "Back");  
             Account account5 = new Account("Google", 12);  
             Account account6 = new Account("Twitter", 11);  
             stu5.setNick(nick5); // One Student To One Nick - One to One Bidirectional - Primary Key  
             stu6.setNick(nick6); // One Student To One Nick - One to One Bidirectional - Primary Key  
             stu5.setAddress(address5); // One Student To One Address - One to One Bidirectional - Foreign Key  
             stu6.setAddress(address6); // One Student To One Address - One to One Bidirectional - Foreign Key  
             stu5.setSchool(school3); // Many Student To One School - Many To One/One To Many Bidirectional  
             stu6.setSchool(school3); // Many Student To One School - Many To One/One To Many Bidirectional  
             stu5.setBike(bike5); // One Student To One Bike - One to One Bidirectional Join Table   
             stu6.setBike(bike6); // One Student To One Bike - One to One Bidirectional Join Table  
             stu5.setGame(game3); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table  
             stu6.setGame(game3); // Many Student To One Game - Many To One/One To Many Bidirectional Join Table  
             Set<Account> setofaccounts3 = new HashSet<Account>();  
             setofaccounts3.add(account5);  
             setofaccounts3.add(account6);  
             stu5.setAccounts(setofaccounts3); // Many Student To Many Account - Many To Many Bidirectional Join Table  
             stu6.setAccounts(setofaccounts3); // Many Student To Many Account - Many To Many Bidirectional Join Table  
             // Persist the domain objects   
             s.save(nick5);  
             s.save(nick6);  
             s.save(address5);  
             s.save(address6);  
             s.save(school3);  
             s.save(bike5);  
             s.save(bike6);  
             s.save(account5);  
             s.save(account6);  
             s.save(game3);  
             s.save(stu5);  
             s.save(stu6);  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         }  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             s = sf.openSession();  
             tx = s.beginTransaction();  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~STUDENT~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             Query query = s.createQuery("From Student");  
             List stulist = query.list();  
             for (Object obj : stulist) {  
                 Student stuq = (Student) obj;  
                 System.out.print(  
                     String.format("♣Student{%s|%s} \t♥Nick{%s|%s} \t♥Address{%s|%s|%s|%s] \t♥School{%s|%s|%s} "  
                             + "\t♠Bike{%s|%s|%s|%s} \t♠Game{%s|%s|%s} \t♠Account[",  
                             stuq.getSid(),   
                             stuq.getSname(),   
                             stuq.getNick().getNid(),   
                             stuq.getNick().getNickName(),  
                             stuq.getAddress().getAdid(),  
                             stuq.getAddress().getStreet(),  
                             stuq.getAddress().getCity(),  
                             stuq.getAddress().getState(),  
                             stuq.getSchool().getSchid(),  
                             stuq.getSchool().getName(),  
                             stuq.getSchool().getEstbYear(),  
                             stuq.getBike().getBikeId(),  
                             stuq.getBike().getBikeNo(),  
                             stuq.getBike().getBikeBrand(),  
                             stuq.getBike().getBikeMfd(),  
                             stuq.getGame().getGameId(),  
                             stuq.getGame().getGameName(),  
                             stuq.getGame().getGamePosition())  
                 );  
                 Set<Account> setOfAccounts = stuq.getAccounts();  
                 for (Account account : setOfAccounts) {  
                     System.out.print(String.format("{%s|%s|%s}",  
                             account.getAcctId(),  
                             account.getAcctType(),  
                             account.getContacts())  
                     );  
                 }  
                 System.out.println("]");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~NICK~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             query = s.createQuery("From Nick");  
             List nicklist = query.list();  
             for (Object obj : nicklist) {  
                 Nick nickq = (Nick) obj;  
                 System.out.println(  
                     String.format("♠Nick{%s|%s} \t♠Student{%s|%s}",  
                             nickq.getNid(),  
                             nickq.getNickName(),  
                             nickq.getStudent().getSid(),  
                             nickq.getStudent().getSname())  
                 );  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ADDRESS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             query = s.createQuery("From Address");  
             List addresslist = query.list();  
             for (Object obj : addresslist) {  
                 Address addressq = (Address) obj;  
                 System.out.println(  
                     String.format("♠Address{%s|%s|%s|%s} \t♠Student{%s|%s}",  
                             addressq.getAdid(),  
                             addressq.getStreet(),  
                             addressq.getCity(),  
                             addressq.getState(),  
                             addressq.getStudent().getSid(),  
                             addressq.getStudent().getSname())  
                 );  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SCHOOL~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             query = s.createQuery("From School");  
             List schoollist = query.list();  
             for (Object obj : schoollist) {  
                 School schoolq = (School) obj;  
                 System.out.print(  
                     String.format("♠School{%s|%s|%s} \t♠Student[",  
                             schoolq.getSchid(),  
                             schoolq.getName(),  
                             schoolq.getEstbYear(),  
                             schoolq.getStudents())  
                 );  
                 Set<Student> setOfStudents = schoolq.getStudents();  
                 for (Student st : setOfStudents) {  
                     System.out.print(  
                         String.format("{%s|%s}",  
                                 st.getSid(),  
                                 st.getSname())  
                     );  
                 }  
                 System.out.println("]");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~BIKE~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             query = s.createQuery("From Bike");  
             List bikelist = query.list();  
             for (Object obj : bikelist) {  
                 Bike bikeq = (Bike) obj;  
                 System.out.println(  
                     String.format("♠Bike{%s|%s|%s|%s} \t♠Student{%s|%s}",  
                             bikeq.getBikeId(),  
                             bikeq.getBikeNo(),  
                             bikeq.getBikeBrand(),  
                             bikeq.getBikeMfd(),  
                             bikeq.getStudent().getSid(),  
                             bikeq.getStudent().getSname())  
                 );  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~GAME~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             query = s.createQuery("From Game");  
             List gamelist = query.list();  
             for (Object obj : gamelist) {  
                 Game gameq = (Game) obj;  
                 System.out.print(  
                     String.format("♠Game{%s|%s|%s} \t♠Student[",  
                             gameq.getGameId(),  
                             gameq.getGameName(),  
                             gameq.getGamePosition(),  
                             gameq.getStudents())  
                 );  
                 Set<Student> setOfStudents = gameq.getStudents();  
                 for (Student st : setOfStudents) {  
                     System.out.print(  
                         String.format("{%s|%s}",  
                                 st.getSid(),  
                                 st.getSname())  
                     );  
                 }  
                 System.out.println("]");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ACCOUNT~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             query = s.createQuery("From Account");  
             List accountlist = query.list();  
             for (Object obj : accountlist) {  
                 Account accountq = (Account) obj;  
                 System.out.print(  
                     String.format("♠Game{%s|%s|%s} \t♠Student[",  
                             accountq.getAcctId(),  
                             accountq.getAcctType(),  
                             accountq.getContacts(),  
                             accountq.getStudents())  
                 );  
                 Set<Student> setOfStudents = accountq.getStudents();  
                 for (Student st : setOfStudents) {  
                     System.out.print(  
                         String.format("{%s|%s}",  
                                 st.getSid(),  
                                 st.getSname())  
                     );  
                 }  
                 System.out.println("]");  
             }  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         } finally {  
             s.close();  
             sf.close();  
         }  
     }  
 } 
Queries
 select * from student;  
 select * from nick;  
 select * from address;  
 select * from school;  
 select * from student;  
 select * from bike;  
 select * from student_bike;  
 select * from student;  
 select * from game;  
 select * from student_game;  
 select * from student;  
 select * from account;  
 select * from student_account;  
 drop table student_account;  
 drop table student_game;  
 drop table student_bike;  
 drop table account;  
 drop table game;  
 drop table bike;  
 drop table school;  
 drop table address;  
 drop table nick;  
 drop table student; 

Check DB :
Download the source here : H03

Thursday, 17 March 2016

H02 Extended : Association Mapping using Hibernate XML - Unidirectional - Using Join Table


Steps:
1. Open the previous project H02.
2. Write more domain classes to extend it.
3. Write corresponding mappings in hbm file.
4. Include the mapping resources in cfg file.
5. Write the client to Insert and Select relationship data into the domain objects.
6. Run the client. And, read the output.

Files, which you have to write:
  1. hibernate.cfg.xml
  2. HibernateUtil.java
  3. com.hibernate.association.Student.java (Update it)
  4. com.hibernate.association.Nick.java (Same as H02)
  5. com.hibernate.association.School.java (Same as H02)
  6. com.hibernate.association.Address.java (Same as H02)
  7. com.hibernate.association.Toy.java (Same as H02)
  8. com.hibernate.association.Bike.java
  9. com.hibernate.association.Watch.java
  10. com.hibernate.association.Game.java
  11. com.hibernate.association.Account.java
  12. com.hibernate.association.student.hbm.xml (Update it to incluce all mappings into this)
  13. H02Extended.java
You will see the Project Structure after adding the files as this :

Codes:
1. hibernate.cfg.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration SYSTEM   
 "file:\\\D:\lib\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/practice</property>  
         <property name="hibernate.connection.username">root</property>  
         <property name="hibernate.connection.password">root</property>  
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
         <property name="hibernate.show_sql">true</property>  
         <property name="hbm2ddl.auto">create</property>  
         <mapping resource="com/hibernate/association/student.hbm.xml" />  
     </session-factory>  
 </hibernate-configuration> 
2. HibernateUtil.java
 package com.hibernate.util;   
  import org.hibernate.SessionFactory;   
  import org.hibernate.cfg.Configuration;   
  public class HibernateUtil {   
    static SessionFactory factory = null;   
    static {   
      try {   
        Configuration cfg = new Configuration();   
        cfg = cfg.configure();   
        factory = cfg.buildSessionFactory();   
      } catch (Exception e) {   
        System.out.println("Error in Initializing SessionFactory");   
        e.printStackTrace();   
      }   
    }   
    public static SessionFactory getSessionFactory() {   
      return factory;   
    }   
  } 
3. Student.java
 package com.hibernate.association;  
 import java.util.Set;  
 /**  
  * Domain class Student  
  *   
  * @author bkar  
  *  
  */  
 public class Student {  
     private int sid;  
     private String sname;  
     // One To One - Primary Key - Unidirectional  
     private Nick name;  
     // One To One - Foreign Key - Unidirectional  
     private School school;  
     // Many To One - Unidirectional  
     private Address address;  
     // One To Many - Unidirectional  
     private Set<Toy> toy;  
     // One To One - Join Table - Unidirectional  
     private Bike bike;  
     // One To Many - Join Table - Unidirectional  
     private Set<Watch> watch;  
     // Many To One - Join Table - Unidirectional  
     private Game game;  
     // Many To Many - Join Table - Unidirectional  
     private Set<Account> account;  
     public Student() {  
         super();  
     }  
     public Student(String sname) {  
         super();  
         this.sname = sname;  
     }  
     // Getters and Setters
 } 
4. Nick.java
 package com.hibernate.association;  
 /**  
  * Domain class attached with Student - One to One - Primary Key - Unidirectional  
  * @author bkar  
  *  
  */  
 public class Nick {  
     private int sid;  
     private String petName;  
     private String natureOf;  
     private boolean naughty;  
     public Nick() {  
         super();  
     }  
     public Nick(String petName, String natureOf, boolean naughty) {  
         super();  
         this.petName = petName;  
         this.natureOf = natureOf;  
         this.naughty = naughty;  
     }  
     // Getters and Setters 
 } 
5. School.java
 package com.hibernate.association;  
 /**  
  * Doamin class attached to Student - One to One - Foreign Key - Unidirectional  
  * @author bkar  
  *  
  */  
 public class School {  
     private int sid;  
     private String name;  
     private String estbYear;  
     public School() {  
         super();  
     }  
     public School(String name, String estbYear) {  
         super();  
         this.name = name;  
         this.estbYear = estbYear;  
     }  
     // Getters and Setters 
 } 
6. Address.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - Many to One - Unidirectional  
  * @author bkar  
  *  
  */  
 public class Address {  
     private int adid;  
     private String street;  
     private String city;  
     private String state;  
     public Address() {  
         super();  
     }  
     public Address(String street, String city, String state) {  
         super();  
         this.street = street;  
         this.city = city;  
         this.state = state;  
     }  
     // Getters and Setters 
 } 
7. Toy.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - One to Many - Unidirectional  
  * @author bkar  
  *  
  */  
 public class Toy {  
     private int toyid;  
     private String toyName;  
     private String toyColor;  
     public Toy() {  
         super();  
     }  
     public Toy(String toyName, String toyColor) {  
         super();  
         this.toyName = toyName;  
         this.toyColor = toyColor;  
     }  
     // Getters and Setters 
     @Override  
     public String toString() {  
         return "Toy("+toyid+")";  
     }  
 } 
8. Bike.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - One to One - Join Table - Unidirectional  
  * @author bkar  
  *  
  */  
 public class Bike {  
     private int bikeId;  
     private String bikeNo;  
     private String bikeBrand;  
     private String bikeMfd;  
     public Bike() {  
         super();  
     }  
     public Bike(String bikeNo, String bikeBrand, String bikeMfd) {  
         super();  
         this.bikeNo = bikeNo;  
         this.bikeBrand = bikeBrand;  
         this.bikeMfd = bikeMfd;  
     }  
     // Getters and Setters 
 } 
9. Watch.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - One to Many - Join Table - Unidirectional  
  * @author bkar  
  *  
  */  
 public class Watch {  
     private int watchId;  
     private String watchType;  
     private String watchBrand;  
     public Watch() {  
         super();  
     }  
     public Watch(String watchType, String watchBrand) {  
         super();  
         this.watchType = watchType;  
         this.watchBrand = watchBrand;  
     }  
     // Getters and Setters
     @Override  
     public String toString() {  
         return "Watch("+watchId+")";  
     }  
 } 
10. Game.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - Many to One - Join table - Unidirectional  
  * @author bkar  
  *  
  */  
 public class Game {  
     private int gameId;  
     private String gameName;  
     private String gamePosition;  
     public Game() {  
         super();  
     }  
     public Game(String gameName, String gamePosition) {  
         super();  
         this.gameName = gameName;  
         this.gamePosition = gamePosition;  
     }  
     // Getters and Setters
 } 
11. Account.java
 package com.hibernate.association;  
 /**  
  * Domain class attached to Student - Many to Many - Join Table - Unidirectional   
  * @author bkar  
  *  
  */  
 public class Account {  
     private int acctId;  
     private String acctType;  
     private int contacts;  
     public Account() {  
         super();  
     }  
     public Account(String acctType, int contacts) {  
         super();  
         this.acctType = acctType;  
         this.contacts = contacts;  
     }  
     // Getters and Setters
     @Override  
     public String toString() {  
         return "Account("+acctId+")";  
     }  
 } 
12. student.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="Student" table="student" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <!-- One To One - Primary Key - Unidirectional -->  
         <one-to-one name="name"/>  
         <!-- One To One - Foreign Key - Unidirectional -->  
         <many-to-one name="school" class="School" column="schid" unique="true" />  
         <!-- Many To One - Unidirectional -->  
         <many-to-one name="address" class="Address" column="adid" />  
         <!-- One To One - Foreign Key - Unidirectional -->  
         <!-- <many-to-one name="address" class="Address" column="adid" unique="true" /> -->  
         <!-- One To Many - Unidirectional -->  
         <set name="toy">  
             <key column="sid"/>  
             <one-to-many class="Toy"/>  
        </set>   
        <!-- One To Many - Join Table - Unidirectional -->  
        <!-- SET should be before JOIN, otherwise validation error-->  
          <set name="watch" table="student_watch" cascade="all">  
               <key column="sid" />  
               <many-to-many column="watchId" class="Watch" unique="true" />  
        </set>  
        <!-- Many To Many - Join Table - Unidirectional -->  
        <!-- SET should be before JOIN, otherwise validation error-->  
          <set name="account" table="student_account" cascade="all">  
               <key column="sid" />  
               <many-to-many column="acctId" class="Account" />  
        </set>  
        <!-- One To One - Join Table - Unidirectional -->  
        <join table="student_bike">  
               <key column="sid" unique="true" />  
               <many-to-one name="bike" column="bikeId" unique="true" />  
          </join>  
          <!-- Many To One - Join Table - Unidirectional -->  
        <join table="student_game">  
            <key column="sid" unique="true"/>  
            <many-to-one name="game" column="gameId"/>  
        </join>  
     </class>  
     <class name="Nick" table="nick" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="petName" column="petName" type="string" />  
         <property name="natureOf" column="natureOf" type="string" />  
         <property name="naughty" column="naughty"/>  
     </class>  
     <class name="School" table="school" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="name" column="name" type="string" />  
         <property name="estbYear" column="estbYear" type="string" />  
     </class>  
     <class name="Address" table="address" lazy="false">  
         <id name="adid" column="adid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="street" column="street" type="string" />  
         <property name="city" column="city" type="string" />  
         <property name="state" column="state" type="string" />  
     </class>  
     <class name="Toy" table="toy" lazy="false">  
         <id name="toyid" column="toyid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="toyName" column="toyName" type="string" />  
         <property name="toyColor" column="toyColor" type="string" />  
     </class>  
     <class name="Bike" table="bike" lazy="false">  
         <id name="bikeId" column="bikeId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="bikeNo" column="bikeNo" type="string" />  
         <property name="bikeBrand" column="bikeBrand" type="string" />  
         <property name="bikeMfd" column="bikeMfd" type="string"/>  
     </class>  
     <class name="Watch" table="watch" lazy="false">  
         <id name="watchId" column="watchId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="watchType" column="watchType" type="string" />  
         <property name="watchBrand" column="watchBrand" type="string" />  
     </class>  
     <class name="Game" table="game" lazy="false">  
         <id name="gameId" column="gameId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="gameName" column="gameName" type="string" />  
         <property name="gamePosition" column="gamePosition" type="string" />  
     </class>  
     <class name="Account" table="account" lazy="false">  
         <id name="acctId" column="acctId" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="acctType" column="acctType" type="string" />  
         <property name="contacts" column="contacts" type="int" />  
     </class>  
 </hibernate-mapping> 
13. H02Extended.java
 package com.client;  
 import java.util.HashSet;  
 import java.util.List;  
 import java.util.Set;  
 import org.hibernate.Query;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import com.hibernate.association.Account;  
 import com.hibernate.association.Address;  
 import com.hibernate.association.Bike;  
 import com.hibernate.association.Game;  
 import com.hibernate.association.Nick;  
 import com.hibernate.association.School;  
 import com.hibernate.association.Student;  
 import com.hibernate.association.Toy;  
 import com.hibernate.association.Watch;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  * Insert and Select Client for  
  * One To One - Primary Key - Unidirectional  
  * One To One - Foreign Key - Unidirectional  
  * Many To One - Unidirectional  
  * One To Many - Unidirectional   
  * One To One - Join Table - Unidirectional  
  * One To Many - join Table - Unidirectional  
  * Many To One - Join Table - Unidirectional  
  * Many To Many - Join Table - Unidirectional  
  * @author bkar  
  */  
 public class H02Extended {  
     public static void main(String[] args) {  
         SessionFactory sf = null;  
         Session s = null;  
         Transaction tx = null;  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             s = sf.openSession();  
             tx = s.beginTransaction();  
             Student stu1 = new Student("Ram");  
             Student stu2 = new Student("Shyam");  
             Student stu3 = new Student("Gopal");  
             Student stu4 = new Student("Rahim");  
             Student stu5 = new Student("Krishna");  
             s.save(stu1);  
             s.save(stu2);  
             s.save(stu3);  
             s.save(stu4);  
             s.save(stu5);  
             Nick nick1 = new Nick("petV", "play0", true);  
             Nick nick2 = new Nick("petA", "play1", true);  
             Nick nick3 = new Nick("petB", "play2", true);  
             Nick nick4 = new Nick("petC", "play3", true);  
             Nick nick5 = new Nick("petD", "play4", true);  
             s.save(nick1);  
             s.save(nick2);  
             s.save(nick3);  
             s.save(nick4);  
             s.save(nick5);  
             School school1 = new School("ABV", "1956");  
             School school2 = new School("CCLHS", "1922");  
             School school3 = new School("RCR", "1911");  
             School school4 = new School("MCR", "1900");  
             School school5 = new School("BITMESRA", "1966");  
             s.save(school1);  
             s.save(school2);  
             s.save(school3);  
             s.save(school4);  
             s.save(school5);  
             Address address = new Address("18", "Bangalore", "KA");  
             s.save(address);  
             Set<Toy> setoftoys = null;  
             for(int i=1;i<=5;i++) {  
                 setoftoys = new HashSet<Toy>();  
                 Toy toy1 = new Toy("Pig", "Pink");  
                 Toy toy2 = new Toy("Cat", "Brown");  
                 Toy toy3 = new Toy("Dog", "Black");  
                 s.save(toy1);  
                 s.save(toy2);  
                 s.save(toy3);  
                 setoftoys.add(toy1);  
                 setoftoys.add(toy2);  
                 setoftoys.add(toy3);  
                 switch(i) {  
                     //4. One To Many - Unidirectional  
                     case 1 : stu1.setToy(setoftoys); break;  
                     case 2 : stu2.setToy(setoftoys); break;  
                     case 3 : stu3.setToy(setoftoys); break;  
                     case 4 : stu4.setToy(setoftoys); break;  
                     case 5 : stu5.setToy(setoftoys); break;  
                 }  
             }  
             //1. One To One - Primary Key - Unidirectional  
             stu1.setName(nick1);  
             // You must have to attach a School to a Student  
             // Otherwise it will throw null pointer exception while iterating, & no transaction will be committed  
             stu2.setName(nick2);  
             stu3.setName(nick3);  
             stu4.setName(nick4);  
             stu5.setName(nick5);  
             //2. One To One - Foreign Key - Unidirectional  
             stu1.setSchool(school1);  
             // You must have to attach a School to a Student  
             // Otherwise it will throw null pointer exception while iterating, & no transaction will be committed  
             stu2.setSchool(school2);  
             stu3.setSchool(school3);  
             stu4.setSchool(school4);  
             stu5.setSchool(school5);  
             //3. Many To One - Unidirectional  
             stu1.setAddress(address);  
             stu2.setAddress(address); // Will throw FK exception in One To One  
             stu3.setAddress(address); // Will throw FK exception in One To One  
             stu4.setAddress(address); // Will throw FK exception in One To One  
             stu5.setAddress(address); // Will throw FK exception in One To One  
             Bike bike1 = new Bike("1111", "Yahama", "2010");  
             Bike bike2 = new Bike("2222", "Bugati", "2011");  
             Bike bike3 = new Bike("3333", "Sukuki", "2012");  
             Bike bike4 = new Bike("4444", "Kawasaki", "2013");  
             Bike bike5 = new Bike("5555", "HD", "2015");  
             s.save(bike1);  
             s.save(bike2);  
             s.save(bike3);  
             s.save(bike4);  
             s.save(bike5);  
             //5. One To One - Join Table - Unidirectional  
             stu1.setBike(bike1);  
             stu2.setBike(bike2);  
             stu3.setBike(bike3);  
             stu4.setBike(bike4);  
             stu5.setBike(bike5);  
             Set<Watch> setofwatchs = null;  
             for(int i=1;i<=5;i++) {  
                 setofwatchs = new HashSet<Watch>();  
                 Watch watch1 = new Watch("Sports","Fastrack");  
                 Watch watch2 = new Watch("Casual","Casio");  
                 Watch watch3 = new Watch("Office","Rado");  
                 Watch watch4 = new Watch("Swimming","Titan");  
                 Watch watch5 = new Watch("Unisex","Fastrack");  
                 s.save(watch1);  
                 s.save(watch2);  
                 s.save(watch3);  
                 s.save(watch4);  
                 s.save(watch5);  
                 setofwatchs.add(watch1);  
                 setofwatchs.add(watch2);  
                 setofwatchs.add(watch3);  
                 setofwatchs.add(watch4);  
                 setofwatchs.add(watch5);  
                 switch(i) {  
                     //6. One To Many - Join Table - Unidirectional  
                     case 1 : stu1.setWatch(setofwatchs); break;  
                     case 2 : stu2.setWatch(setofwatchs); break;  
                     case 3 : stu3.setWatch(setofwatchs); break;  
                     case 4 : stu4.setWatch(setofwatchs); break;  
                     case 5 : stu5.setWatch(setofwatchs); break;  
                 }  
             }  
             Game game1 = new Game("Cricket", "Batsman");  
             s.save(game1);  
             //7. Many To One - Join Table - Unidirectional  
             stu1.setGame(game1);  
             stu2.setGame(game1);  
             stu3.setGame(game1);  
             stu4.setGame(game1);  
             stu5.setGame(game1);  
             Set<Account> setofaccounts = null;  
             Account acctAll1 = new Account("ForAll1",12);  
             Account acctAll2 = new Account("ForAll2",13);  
             Account acctAll3 = new Account("ForAll3",14);  
             s.save(acctAll1);  
             s.save(acctAll2);  
             s.save(acctAll3);  
             for(int i=1;i<=5;i++) {  
                 setofaccounts = new HashSet<Account>();  
                 Account account1 = new Account("Facebook", 32);  
                 Account account2 = new Account("Gmail", 30);  
                 Account account3 = new Account("Yahoo", 24);  
                 Account account4 = new Account("Bing", 16);  
                 Account account5 = new Account("MySpace", 8);  
                 s.save(account1);  
                 s.save(account2);  
                 s.save(account3);  
                 s.save(account4);  
                 s.save(account5);  
                 setofaccounts.add(account1);  
                 setofaccounts.add(account2);  
                 setofaccounts.add(account3);  
                 setofaccounts.add(account4);  
                 setofaccounts.add(account5);  
                 setofaccounts.add(acctAll1);  
                 setofaccounts.add(acctAll2);  
                 setofaccounts.add(acctAll3);  
                 switch(i) {  
                     //8. Many To Many - Join Table - Unidirectional  
                     case 1 : stu1.setAccount(setofaccounts); break;  
                     case 2 : stu2.setAccount(setofaccounts); break;  
                     case 3 : stu3.setAccount(setofaccounts); break;  
                     case 4 : stu4.setAccount(setofaccounts); break;  
                     case 5 : stu5.setAccount(setofaccounts); break;  
                 }  
             }  
             Query query = s.createQuery("From Student");  
             List stul = query.list();  
             for (Object obj : stul) {  
                 Student stu6 = (Student) obj;  
                 System.out.println("********************************"  
                     + "\n Student: "+ stu6.getSid()   
                     + "\n Student: "+ stu6.getSname()   
                     + "\n Nick: "  + stu6.getName().getSid()      
                     + "\n Nick: "  + stu6.getName().getPetName()  
                     + "\n Nick: "  + stu6.getName().getNatureOf()  
                     + "\n Nick: "  + stu6.getName().isNaughty()  
                     + "\n School: " + stu6.getSchool().getSid()  
                     + "\n School: " + stu6.getSchool().getName()  
                     + "\n School: " + stu6.getSchool().getEstbYear()  
                     + "\n Address: "+ stu6.getAddress().getAdid()   
                     + "\n Address: "+ stu6.getAddress().getStreet()   
                     + "\n Address: "+ stu6.getAddress().getCity()   
                     + "\n Address: "+ stu6.getAddress().getState()  
                     + "\n Toy: "  + stu6.getToy()  
                     + "\n Bike: "  + stu6.getBike().getBikeId()  
                     + "\n Bike: "  + stu6.getBike().getBikeNo()  
                     + "\n Bike: "  + stu6.getBike().getBikeBrand()  
                     + "\n Bike: "  + stu6.getBike().getBikeMfd()  
                     + "\n Watch: " + stu6.getWatch()  
                     + "\n Game: "  + stu6.getGame().getGameId()  
                     + "\n Game: "  + stu6.getGame().getGameName()  
                     + "\n Game: "  + stu6.getGame().getGamePosition()  
                     + "\n Account: "+ stu6.getAccount()  
                     );  
                 Set<Toy> setOfToys = stu6.getToy();  
                 for (Toy t : setOfToys) {  
                     System.out.println(  
                         " Toy ID : "   + t.getToyid()  
                         +"\t Toy Name : " + t.getToyName()  
                         +"\t Toy Color : "+ t.getToyColor()  
                     );  
                 }  
                 Set<Watch> setOfWatchs = stu6.getWatch();  
                 for (Watch w : setOfWatchs) {  
                     System.out.println(  
                         " Watch ID : "   + w.getWatchId()  
                         +"\t Watch Type : " + w.getWatchType()  
                         +"\t Watch Brand : "+ w.getWatchBrand()  
                     );  
                 }  
                 Set<Account> setOfAccounts = stu6.getAccount();  
                 for (Account a : setOfAccounts) {  
                     System.out.println(  
                         " Account ID : "     + a.getAcctId()  
                         +"\t Account Type : "  + a.getAcctType()  
                         +"\t Account Contacts : "+ a.getContacts()  
                     );  
                 }  
                 System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");  
             }  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         } finally {  
             s.close();  
             sf.close();  
         }  
     }  
 } 

Download the Source code here : H02 Extended

Saturday, 12 March 2016

Hibernate Facts

Generally, we write one mapping file (.hbm) for every domain class, while using Hibernate Core.
But, writing so is not necessary.

We can write multiple classes' mapping in one mapping file.
Many classes written in one package can be mapped in one .hbm file.
You cannot write multiple packages in one .hbm file.

Difference between Unidirectional and Bidirectional Mapping :
As the name shows,
Unidirectional means the relationship between two domains is visible only in one directional.
e.g. Student to Address unidirectional relationship means Address can be retrieved using Student object.
But, Students attached to Address won't be retrieved using Address object.
So, the relationship flows only in one direction.

Bidirectional means the relationship between domains flows in both direction.
e.g. Student to Address bidirectional relationship means we can fetch,
Address related to a Student using Student object. &
Student related to a Address using Address object.

Its like a restriction, if we want to user to fetch the reverse relationship or not.
For bidirectional relationship, we have to define the mapping in both domain classes.


AnnotationConfiguration got deprecated
Reference : http://www.mkyong.com/hibernate/hibernate-the-type-annotationconfiguration-is-deprecated/

Working with Hibernate 3.6, noticed the previous “org.hibernate.cfg.AnnotationConfiguration“, is marked as “deprecated“.
I felt this on 03-April-2016 (5 years late), when I havn't found any class for AnnotationConfiguration in Hibernate 5.1

In Hibernate 3.6, “org.hibernate.cfg.AnnotationConfiguration” is deprecated, and all its functionality has been moved to “org.hibernate.cfg.Configuration“.
So , you can safely replace your “AnnotationConfiguration” with “Configuration” class.


H02 : Association Mapping using Hibernate XML - Unidirectional


Steps in Eclipse MARS :
1. Create a Java Project "H02'.
2. Place mysql jar in build path.
3. Place Hibernate 5 required jars in build path.
4. Write Domain classes, Hibernate configuration, Util, Hibernate Mappings, and, Clients.

You can see the project as :


Files, which you have to write:
  1. hibernate.cfg.xml
  2. HibernateUtil.java
  3. com.hibernate.association.Student.java
  4. com.hibernate.association.Nick.java
  5. com.hibernate.association.School.java
  6. com.hibernate.association.Address.java
  7. com.hibernate.association.Toy.java
  8. com.hibernate.association.student.hbm.xml
  9. com.hibernate.association.nick.hbm.xml
  10. com.hibernate.association.school.hbm.xml
  11. com.hibernate.association.address.hbm.xml
  12. com.hibernate.association.toy.hbm.xml
  13. H02.java
Codes of H02:
1. hibernate.cfg.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration SYSTEM   
 "file:\\\D:\lib\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/practice</property>  
         <property name="hibernate.connection.username">root</property>  
         <property name="hibernate.connection.password">root</property>  
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
         <property name="hibernate.show_sql">true</property>  
         <property name="hbm2ddl.auto">create</property>  
         <mapping resource="com/hibernate/association/student.hbm.xml" />  
         <mapping resource="com/hibernate/association/nick.hbm.xml" />  
         <mapping resource="com/hibernate/association/school.hbm.xml" />  
         <mapping resource="com/hibernate/association/address.hbm.xml" />  
         <mapping resource="com/hibernate/association/toy.hbm.xml" />  
     </session-factory>  
 </hibernate-configuration> 
2. HibernateUtil.java
 package com.hibernate.util;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.cfg.Configuration;  
 public class HibernateUtil {  
     static SessionFactory factory = null;  
     static {  
         try {  
             Configuration cfg = new Configuration();  
             cfg = cfg.configure();  
             factory = cfg.buildSessionFactory();  
         } catch (Exception e) {  
             System.out.println("Error in Initializing SessionFactory");  
             e.printStackTrace();  
         }  
     }  
     public static SessionFactory getSessionFactory() {  
         return factory;  
     }  
 } 
3. Student.java
 package com.hibernate.association;  
 import java.util.Set;  
 public class Student {  
     private int sid;  
     private String sname;  
     // One To One - Primary Key - Unidirectional  
     private Nick name;  
     // One To One - Foreign Key - Unidirectional  
     private School school;  
     // Many To One - Unidirectional  
     private Address address;  
     // One To Many - Unidirectional  
     private Set<Toy> toy;  
     public Student() {  
         super();  
     }  
     public Student(String sname) {  
         super();  
         this.sname = sname;  
     }  
     // Getters and Setters  
 } 
4. Nick.java
 package com.hibernate.association;  
 public class Nick {  
     private int sid;  
     private String petName;  
     private String natureOf;  
     private boolean naughty;  
     public Nick() {  
         super();  
     }  
     public Nick(String petName, String natureOf, boolean naughty) {  
         super();  
         this.petName = petName;  
         this.natureOf = natureOf;  
         this.naughty = naughty;  
     }  
     // Getters and Setters  
 } 
5. School.java
 package com.hibernate.association;  
 public class School {  
     private int sid;  
     private String name;  
     private String estbYear;  
     public School() {  
         super();  
     }  
     public School(String name, String estbYear) {  
         super();  
         this.name = name;  
         this.estbYear = estbYear;  
     }  
     // Getters and Setters  
 } 
6. Address.java
 package com.hibernate.association;  
 public class Address {  
     private int adid;  
     private String street;  
     private String city;  
     private String state;  
     public Address() {  
         super();  
     }  
     public Address(String street, String city, String state) {  
         super();  
         this.street = street;  
         this.city = city;  
         this.state = state;  
     }  
     // Getters and Setters  
 } 
7. Toy.java
 package com.hibernate.association;  
 public class Toy {  
     private int toyid;  
     private String toyName;  
     private String toyColor;  
     public Toy() {  
         super();  
     }  
     public Toy(String toyName, String toyColor) {  
         super();  
         this.toyName = toyName;  
         this.toyColor = toyColor;  
     }  
     // Getters and Setters  
 } 
8. student.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="Student" table="student" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <!-- One To One - Primary Key - Unidirectional -->  
         <one-to-one name="name"/>  
         <!-- One To One - Foreign Key - Unidirectional -->  
         <many-to-one name="school" class="School" column="schid" unique="true" />  
         <!-- Many To One - Unidirectional -->  
         <many-to-one name="address" class="Address" column="adid" />  
         <!-- One To One - Foreign Key - Unidirectional -->  
         <!-- <many-to-one name="address" class="Address" column="adid" unique="true" /> -->  
         <!-- One To Many - Unidirectional -->  
         <set name="toy">  
             <key column="sid"/>  
             <one-to-many class="Toy"/>  
        </set>   
     </class>  
 </hibernate-mapping> 
9. nick.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="Nick" table="nick" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="petName" column="petName" type="string" />  
         <property name="natureOf" column="natureOf" type="string" />  
         <property name="naughty" column="naughty"/>  
     </class>  
 </hibernate-mapping> 
10. school.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="School" table="school" lazy="false">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="name" column="name" type="string" />  
         <property name="estbYear" column="estbYear" type="string" />  
     </class>  
 </hibernate-mapping> 
11. address.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="Address" table="address" lazy="false">  
         <id name="adid" column="adid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="street" column="street" type="string" />  
         <property name="city" column="city" type="string" />  
         <property name="state" column="state" type="string" />  
     </class>  
 </hibernate-mapping> 
12. toy.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.association">  
     <class name="Toy" table="toy" lazy="false">  
         <id name="toyid" column="toyid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="toyName" column="toyName" type="string" />  
         <property name="toyColor" column="toyColor" type="string" />  
     </class>  
 </hibernate-mapping> 
13. H02.java
 package com.client;  
 import java.util.HashSet;  
 import java.util.List;  
 import java.util.Set;  
 import org.hibernate.Query;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import com.hibernate.association.Address;  
 import com.hibernate.association.Nick;  
 import com.hibernate.association.School;  
 import com.hibernate.association.Student;  
 import com.hibernate.association.Toy;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  * Insert and Select Client for 
  * One To One - Primary Key - Unidirectional
  * One To One - Foreign Key - Unidirectional
  * Many To One - Unidirectional
  * One To Many - Unidirectional  
  * @author bkar  
  */  
 public class H02 {  
     public static void main(String[] args) {  
         SessionFactory sf = null;  
         Session s = null;  
         Transaction tx = null;  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             s = sf.openSession();  
             tx = s.beginTransaction();  
             
             Student stu1 = new Student("Ram");  
             Student stu2 = new Student("Shyam");  
             Student stu3 = new Student("Gopal");  
             Student stu4 = new Student("Rahim");  
             Student stu5 = new Student("Krishna");  
             s.save(stu1);  
             s.save(stu2);  
             s.save(stu3);  
             s.save(stu4);  
             s.save(stu5);  
             
             Nick nick1 = new Nick("petV", "play0", true);  
             Nick nick2 = new Nick("petA", "play1", true);  
             Nick nick3 = new Nick("petB", "play2", true);  
             Nick nick4 = new Nick("petC", "play3", true);  
             Nick nick5 = new Nick("petD", "play4", true);  
             s.save(nick1);  
             s.save(nick2);  
             s.save(nick3);  
             s.save(nick4);  
             s.save(nick5);  
             
             School school1 = new School("ABV", "1956");  
             School school2 = new School("CCLHS", "1922");  
             School school3 = new School("RCR", "1911");  
             School school4 = new School("MCR", "1900");  
             School school5 = new School("BITMESRA", "1966");  
             s.save(school1);  
             s.save(school2);  
             s.save(school3);  
             s.save(school4);  
             s.save(school5);  
             
             Address address = new Address("18", "Bangalore", "KA");  
             s.save(address);  
             
             Set<Toy> setoftoys = null;  
             for(int i=1;i<=5;i++) {  
                 setoftoys = new HashSet<Toy>();  
                 Toy toy1 = new Toy("Pig", "Pink");  
                 Toy toy2 = new Toy("Cat", "Brown");  
                 Toy toy3 = new Toy("Dog", "Black");  
                 s.save(toy1);  
                 s.save(toy2);  
                 s.save(toy3);  
                 setoftoys.add(toy1);  
                 setoftoys.add(toy2);  
                 setoftoys.add(toy3);  
                 switch(i) {  
                     case 1 : stu1.setToy(setoftoys); break;  
                     case 2 : stu2.setToy(setoftoys); break;  
                     case 3 : stu3.setToy(setoftoys); break;  
                     case 4 : stu4.setToy(setoftoys); break;  
                     case 5 : stu5.setToy(setoftoys); break;  
                 }  
             }  
             
             // One To One - Primary Key - Unidirectional  
             stu1.setName(nick1);  
             // You must have to attach a School to a Student  
             // Otherwise it will throw null pointer exception while iterating, & no trans will be committed  
             stu2.setName(nick2);  
             stu3.setName(nick3);  
             stu4.setName(nick4);  
             stu5.setName(nick5);  
             
             // One To One - Foreign Key - Unidirectional  
             stu1.setSchool(school1);  
             // You must have to attach a School to a Student  
             // Otherwise it will throw null pointer exception while iterating, & no trans will be committed  
             stu2.setSchool(school2);  
             stu3.setSchool(school3);  
             stu4.setSchool(school4);  
             stu5.setSchool(school5);  
             
             // Many To One - Unidirectional  
             stu1.setAddress(address);  
             stu2.setAddress(address); // Will throw FK exception in One To One  
             stu3.setAddress(address); // Will throw FK exception in One To One  
             stu4.setAddress(address); // Will throw FK exception in One To One  
             stu5.setAddress(address); // Will throw FK exception in One To One  
             
             /*  
             // One To Many - Unidirectional  
             stu1.setToy(setoftoys);  
             stu2.setToy(setoftoys);  
             stu3.setToy(setoftoys);  
             stu4.setToy(setoftoys);  
             stu5.setToy(setoftoys);*/  
             
             Query query = s.createQuery("From Student");  
             List stul = query.list();  
             for (Object obj : stul) {  
                 Student stu6 = (Student) obj;  
                 System.out.println("********************************"  
                     + "\n Student: "+ stu6.getSid()   
                     + "\n Student: "+ stu6.getSname()   
                     + "\n Nick: "  + stu6.getName().getSid()      
                     + "\n Nick: "  + stu6.getName().getPetName()  
                     + "\n Nick: "  + stu6.getName().getNatureOf()  
                     + "\n Nick: "  + stu6.getName().isNaughty()  
                     + "\n School: " + stu6.getSchool().getSid()  
                     + "\n School: " + stu6.getSchool().getName()  
                     + "\n School: " + stu6.getSchool().getEstbYear()  
                     + "\n Address: "+ stu6.getAddress().getAdid()   
                     + "\n Address: "+ stu6.getAddress().getStreet()   
                     + "\n Address: "+ stu6.getAddress().getCity()   
                     + "\n Address: "+ stu6.getAddress().getState()  
                     + "\n Toy: "  + stu6.getToy()  
                     );  
                 Set<Toy> setOfToys = stu6.getToy();  
                 for (Toy t : setOfToys) {  
                     System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~"  
                         +" Toy ID : "   + t.getToyid()  
                         +"\t Toy Name : " + t.getToyName()  
                         +"\t Toy Color : "+ t.getToyColor()  
                     );  
                 }  
             }  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         } finally {  
             s.close();  
             sf.close();  
         }  
     }  
 } 
Query
 delimiter $
 select * from student;
 select * from nick;
 select * from school;
 select * from address;
 select * from toy;
 $

Monday, 7 March 2016

H01 Extended : to include Simple, Collection, and, 3 types of Inheritance Mapping


Steps in Eclipse MARS :
1. Open the java project created in previod example "H01".
2. Lets keep the mysql jar in build path.
3. Lets keep the Hibernate 5 required jars in build path.
4. Extend the project to add package for domain classes for Collection and Inheritance mappings.

You can see the project as :


Files, which you have to write:
  1. hibernate.cfg.xml
  2. HibernateUtil.java
  3. com.hibernate.simple.Customer.java
  4. com.hibernate.simple.customer.hbm.xml
  5. H01Client1.java - For Simple Mapping

  6. com.hibernate.collection.Student.java
  7. com.hibernate.collection.student.hbm.xml
  8. H01Client2.java - For Collection Mapping

  9. com.hibernate.inheritance.Student.java
  10. com.hibernate.inheritance.HighSchool.java
  11. com.hibernate.inheritance.PrimarySchool.java
  12. com.hibernate.inheritance.YellowTagged.java
  13. com.hibernate.inheritance.RedTagged.java
  14. com.hibernate.inheritance.Genius.java
  15. com.hibernate.inheritance.Brianiac.java
  16. com.hibernate.inheritance.Wicked.java
  17. com.hibernate.inheritance.Fighter.java
  18. com.hibernate.inheritance.student1.hbm.xml
  19. com.hibernate.inheritance.student2.hbm.xml
  20. com.hibernate.inheritance.student3.hbm.xml
  21. H01InheritanceInsert.java
  22. H01InheritanceSelect.java
Codes of H01:
1. hibernate.cfg.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-configuration SYSTEM   
 "file:\\\D:\lib\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/practice</property>  
         <property name="hibernate.connection.username">root</property>  
         <property name="hibernate.connection.password">root</property>  
         <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>  
         <property name="hibernate.show_sql">true</property>  
         <property name="hbm2ddl.auto">create</property>  
         <mapping resource="com/hibernate/simple/customer.hbm.xml" />  
         <mapping resource="com/hibernate/collection/student.hbm.xml" />  
         <mapping resource="com/hibernate/inheritance/student2.hbm.xml" />  
         <mapping resource="com/hibernate/inheritance/student.hbm.xml" />  
     </session-factory>  
 </hibernate-configuration> 
2. HibernateUtil.java
 package com.hibernate.util;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.cfg.Configuration;  
 public class HibernateUtil {  
     static SessionFactory factory = null;  
     static {  
         try {  
             Configuration cfg = new Configuration();  
             cfg = cfg.configure();  
             factory = cfg.buildSessionFactory();  
         } catch (Exception e) {  
             System.out.println("Error in Initializing SessionFactory");  
             e.printStackTrace();  
         }  
     }  
     public static SessionFactory getSessionFactory() {  
         return factory;  
     }  
 } 
3. Customer.java
 package com.hibernate.simple;  
 /**  
  * A Domain class, which demonstrates the Hibernate Core Simple Mapping  
  * @author bkar  
  *  
  */  
 public class Customer {  
     private String cname;  
     private int cid;  
     public Customer() {  
         super();  
     }  
     public Customer(String cname) {  
         super();  
         this.cname = cname;  
     }  
     public Customer(String cname, int cid) {  
         super();  
         this.cname = cname;  
         this.cid = cid;  
     }  
     public String getCname() {  
         return cname;  
     }  
     public void setCname(String cname) {  
         this.cname = cname;  
     }  
     public int getCid() {  
         return cid;  
     }  
     public void setCid(int cid) {  
         this.cid = cid;  
     }  
     @Override  
     public String toString() {  
         return "Customer[]" + "cid: " + cid + "cname: " + cname;  
     }  
 } 
4. customer.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.simple">  
     <class name="Customer" table="cust">  
         <id name="cid" column="cid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="cname" type="string" column="cname"/>  
     </class>  
 </hibernate-mapping> 
5. H01Client1.java
 package com.client;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import com.hibernate.simple.Customer;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  *   
  * Client code to demonstrate persistence of domain class having Simple datatypes  
  * @author bkar  
  *  
  */  
 public class H01Client1 {  
     public static void main(String args[]) {  
         Transaction tx = null;  
         SessionFactory fact = null;  
         Session session = null;  
         try {  
             fact = HibernateUtil.getSessionFactory();  
             session = fact.openSession();  
             tx = session.beginTransaction();  
             Customer cust = new Customer("bajrang");  
             Integer in = (Integer) session.save(cust);  
             int inti = in.intValue();  
             System.out.println(inti);  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         } finally {  
             session.close();  
             fact.close();  
         }  
     }  
 } 
6. Student.java
 package com.hibernate.collection;  
 import java.util.*;  
 /**  
  * Domain class to demonstrate, Hibernate 5 Collection Mapping using XML  
  * @author bkar  
  *  
  */  
 public class Student {  
     private int sid;  
     private String dob;  
     private String qualification;  
     private String sname;  
     private String[] courses;  
     private List<String> emails;  
     private List<Integer> marks;  
     private Set<Long> phones;  
     private Map<String, Long> refs;  
     public Student() {  
     }  
     public Student(String sname, String dob, String qualification, String[] courses, List<String> emails,  
             List<Integer> marks, Set<Long> phones, Map<String, Long> refs) {  
         this.dob = dob;  
         this.qualification = qualification;  
         this.sname = sname;  
         this.courses = courses;  
         this.emails = emails;  
         this.marks = marks;  
         this.phones = phones;  
         this.refs = refs;  
     }  
     public int getSid() {  
         return sid;  
     }  
     public void setSid(int sid) {  
         this.sid = sid;  
     }  
     public String getDob() {  
         return dob;  
     }  
     public void setDob(String dob) {  
         this.dob = dob;  
     }  
     public String getQualification() {  
         return qualification;  
     }  
     public void setQualification(String qualification) {  
         this.qualification = qualification;  
     }  
     public String getSname() {  
         return sname;  
     }  
     public void setSname(String sname) {  
         this.sname = sname;  
     }  
     public String[] getCourses() {  
         return courses;  
     }  
     public void setCourses(String[] courses) {  
         this.courses = courses;  
     }  
     public List<String> getEmails() {  
         return emails;  
     }  
     public void setEmails(List<String> emails) {  
         this.emails = emails;  
     }  
     public List<Integer> getMarks() {  
         return marks;  
     }  
     public void setMarks(List<Integer> marks) {  
         this.marks = marks;  
     }  
     public Set<Long> getPhones() {  
         return phones;  
     }  
     public void setPhones(Set<Long> phones) {  
         this.phones = phones;  
     }  
     public Map<String, Long> getRefs() {  
         return refs;  
     }  
     public void setRefs(Map<String, Long> refs) {  
         this.refs = refs;  
     }  
 } 
7. student.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.collection">  
     <class name="Student" table="students">  
         <id name="sid" column="sid" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" />  
         <property name="dob"/>  
         <property name="qualification"/>  
         <array name="courses" table="courses">  
             <key column="sid" />  
             <index column="idx"/>  
             <element column="cname" type="string" />  
         </array>  
         <list name="emails" table="emails">  
             <key column="sid" />  
             <index column="idx"/>  
             <element column="emailId" type="string" />  
         </list>  
         <bag name="marks" table="marks">  
             <key column="sid" />  
             <element column="marks" type="int" />  
         </bag>  
         <set name="phones" table="phones">  
             <key column="sid" />  
             <element column="phoneNo" type="long" />  
         </set>  
         <map name="refs" table="refs">  
             <key column="sid" />  
             <index column="sname" type="string"/>  
             <element column="sphone" type="long" />  
         </map>  
     </class>  
 </hibernate-mapping> 
8. H01Client2.java
 package com.client;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.HashSet;  
 import java.util.List;  
 import java.util.Map;  
 import java.util.Set;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import com.hibernate.collection.Student;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  * Client code for persisting domain class having complex Collection variables  
  * @author bkar  
  *  
  */  
 public class H01Client2 {  
     public static void main(String[] args) {  
         Transaction tx = null;  
         SessionFactory sf = null;  
         Session session = null;  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             session = sf.openSession();  
             tx = session.beginTransaction();  
             String courses[] = { "Java", "JDBC", "JSP", "JPA", "Hibernate", "EJB" };  
             List<String> emails = new ArrayList<String>();  
             emails.add("aa@j2ee.com");  
             emails.add("bb@j2ee.com");  
             emails.add("bb@j2ee.com");  
             List<Integer> marks = new ArrayList<Integer>();  
             marks.add(new Integer(700));  
             marks.add(new Integer(699));  
             marks.add(new Integer(800));  
             Set<Long> phones = new HashSet<Long>();  
             phones.add(new Long(232323));  
             phones.add(new Long(112233));  
             phones.add(new Long(123456));  
             Map<String, Long> refers = new HashMap<String, Long>();  
             refers.put("aaa", new Long(464646));  
             refers.put("bbb", new Long(224466));  
             refers.put("ccc", new Long(246810));  
             Student stud = new Student("J2EE", "10-10-10", "B.Sc.", courses, emails, marks, phones, refers);  
             Integer in = (Integer) session.save(stud);  
             int sid = in.intValue();  
             System.out.println(sid);  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         } finally {  
             session.close();  
             sf.close();  
         }  
     }  
 } 
9. Student.java
 package com.hibernate.inheritance;  
 /**  
  * Student domain class  
  * @author bkar  
  *  
  */  
 public class Student {  
     private int sroll;  
     private String sname;  
     private String gender;  
     public Student(String sname, String gender) {  
         super();  
         this.sname = sname;  
         this.gender = gender;  
     }  
     public int getSroll() {  
         return sroll;  
     }  
     public void setSroll(int sroll) {  
         this.sroll = sroll;  
     }  
     public String getSname() {  
         return sname;  
     }  
     public void setSname(String sname) {  
         this.sname = sname;  
     }  
     public String getGender() {  
         return gender;  
     }  
     public void setGender(String gender) {  
         this.gender = gender;  
     }  
 } 
10. HighSchool.java
 package com.hibernate.inheritance;  
 /**  
  * HighSchool domain class  
  * @author bkar  
  *  
  */  
 public class HighSchool extends Student {  
     private int hroll;  
     private String hname;  
     private String game;  
     private boolean inlove;  
     private int arrestcount;  
     public HighSchool(String sname, String gender, int hroll, String hname, String game, boolean inlove,  
             int arrestcount) {  
         super(sname, gender);  
         this.hroll = hroll;  
         this.hname = hname;  
         this.game = game;  
         this.inlove = inlove;  
         this.arrestcount = arrestcount;  
     }  
     public int getHroll() {  
         return hroll;  
     }  
     public void setHroll(int hroll) {  
         this.hroll = hroll;  
     }  
     public String getHname() {  
         return hname;  
     }  
     public void setHname(String hname) {  
         this.hname = hname;  
     }  
     public String getGame() {  
         return game;  
     }  
     public void setGame(String game) {  
         this.game = game;  
     }  
     public boolean isInlove() {  
         return inlove;  
     }  
     public void setInlove(boolean inlove) {  
         this.inlove = inlove;  
     }  
     public int getArrestcount() {  
         return arrestcount;  
     }  
     public void setArrestcount(int arrestcount) {  
         this.arrestcount = arrestcount;  
     }  
 } 
11. PrimarySchool.java
 package com.hibernate.inheritance;  
 /**  
  * PrimarySchool domain class  
  * @author bkar  
  *  
  */  
 public class PrimarySchool extends Student {  
     private int proll;  
     private String beltcolor;  
     private String tiecolor;  
     public PrimarySchool(String sname, String gender, int proll, String beltcolor, String tiecolor) {  
         super(sname, gender);  
         this.proll = proll;  
         this.beltcolor = beltcolor;  
         this.tiecolor = tiecolor;  
     }  
     public int getProll() {  
         return proll;  
     }  
     public void setProll(int proll) {  
         this.proll = proll;  
     }  
     public String getBeltcolor() {  
         return beltcolor;  
     }  
     public void setBeltcolor(String beltcolor) {  
         this.beltcolor = beltcolor;  
     }  
     public String getTiecolor() {  
         return tiecolor;  
     }  
     public void setTiecolor(String tiecolor) {  
         this.tiecolor = tiecolor;  
     }  
 } 
12. YellowTagged.java
 package com.hibernate.inheritance;  
 /**  
  * YellowTagged domain class  
  * @author bkar  
  *  
  */  
 public class YellowTagged extends PrimarySchool {  
     private int yroll;  
     private String yname;  
     private boolean bookworm;  
     private int iq;  
     public YellowTagged(String sname, String gender, int proll, String beltcolor, String tiecolor, int yroll,  
             String yname, boolean bookworm, int iq) {  
         super(sname, gender, proll, beltcolor, tiecolor);  
         this.yroll = yroll;  
         this.yname = yname;  
         this.bookworm = bookworm;  
         this.iq = iq;  
     }  
     public int getYroll() {  
         return yroll;  
     }  
     public void setYroll(int yroll) {  
         this.yroll = yroll;  
     }  
     public String getYname() {  
         return yname;  
     }  
     public void setYname(String yname) {  
         this.yname = yname;  
     }  
     public boolean isBookworm() {  
         return bookworm;  
     }  
     public void setBookworm(boolean bookworm) {  
         this.bookworm = bookworm;  
     }  
     public int getIq() {  
         return iq;  
     }  
     public void setIq(int iq) {  
         this.iq = iq;  
     }  
 } 
13. RedTagged.java
 package com.hibernate.inheritance;  
 /**  
  * RedTagged domain class  
  * @author bkar  
  *  
  */  
 public class RedTagged extends PrimarySchool {  
     private int rcount;  
     private String fname;  
     private int fightfought;  
     private int toothbroken;  
     public RedTagged(String sname, String gender, int proll, String beltcolor, String tiecolor, int rcount,  
             String fname, int fightfought, int toothbroken) {  
         super(sname, gender, proll, beltcolor, tiecolor);  
         this.rcount = rcount;  
         this.fname = fname;  
         this.fightfought = fightfought;  
         this.toothbroken = toothbroken;  
     }  
     public int getRcount() {  
         return rcount;  
     }  
     public void setRcount(int rcount) {  
         this.rcount = rcount;  
     }  
     public String getFname() {  
         return fname;  
     }  
     public void setFname(String fname) {  
         this.fname = fname;  
     }  
     public int getFightfought() {  
         return fightfought;  
     }  
     public void setFightfought(int fightfought) {  
         this.fightfought = fightfought;  
     }  
     public int getToothbroken() {  
         return toothbroken;  
     }  
     public void setToothbroken(int toothbroken) {  
         this.toothbroken = toothbroken;  
     }  
 } 
14. Genius.java
 package com.hibernate.inheritance;  
 /**  
  * Genius domain class  
  * @author bkar  
  *  
  */  
 public class Genius extends YellowTagged {  
     private int getDumpedCount;  
     private int crushCount;  
     private int crashCount;  
     public Genius(String sname, String gender, int proll, String beltcolor, String tiecolor, int yroll, String yname,  
             boolean bookworm, int iq, int getDumpedCount, int crushCount, int crashCount) {  
         super(sname, gender, proll, beltcolor, tiecolor, yroll, yname, bookworm, iq);  
         this.getDumpedCount = getDumpedCount;  
         this.crushCount = crushCount;  
         this.crashCount = crashCount;  
     }  
     public int getGetDumpedCount() {  
         return getDumpedCount;  
     }  
     public void setGetDumpedCount(int getDumpedCount) {  
         this.getDumpedCount = getDumpedCount;  
     }  
     public int getCrushCount() {  
         return crushCount;  
     }  
     public void setCrushCount(int crushCount) {  
         this.crushCount = crushCount;  
     }  
     public int getCrashCount() {  
         return crashCount;  
     }  
     public void setCrashCount(int crashCount) {  
         this.crashCount = crashCount;  
     }  
 } 
15. Brainiac.java
 package com.hibernate.inheritance;  
 /**  
  * Brainiac domain class  
  * @author bkar  
  *  
  */  
 public class Brainiac extends YellowTagged {  
     private long readBookCount;  
     private int yellowTeethCount;  
     private long getBeatenByFightersCount;  
     public Brainiac(String sname, String gender, int proll, String beltcolor, String tiecolor, int yroll, String yname,  
             boolean bookworm, int iq, long readBookCount, int yellowTeethCount, long getBeatenByFightersCount) {  
         super(sname, gender, proll, beltcolor, tiecolor, yroll, yname, bookworm, iq);  
         this.readBookCount = readBookCount;  
         this.yellowTeethCount = yellowTeethCount;  
         this.getBeatenByFightersCount = getBeatenByFightersCount;  
     }  
     public long getReadBookCount() {  
         return readBookCount;  
     }  
     public void setReadBookCount(long readBookCount) {  
         this.readBookCount = readBookCount;  
     }  
     public int getYellowTeethCount() {  
         return yellowTeethCount;  
     }  
     public void setYellowTeethCount(int yellowTeethCount) {  
         this.yellowTeethCount = yellowTeethCount;  
     }  
     public long getGetBeatenByFightersCount() {  
         return getBeatenByFightersCount;  
     }  
     public void setGetBeatenByFightersCount(long getBeatenByFightersCount) {  
         this.getBeatenByFightersCount = getBeatenByFightersCount;  
     }  
 } 
16. Wicked.java
 package com.hibernate.inheritance;  
 /**  
  * Wicked domain class  
  * @author bkar  
  *  
  */  
 public class Wicked extends RedTagged {  
     private String speciality;  
     private int fightOrganised;  
     private int getCaught;  
     private int successRate;  
     public Wicked(String sname, String gender, int proll, String beltcolor, String tiecolor, int rcount, String fname,  
             int fightfought, int toothbroken, String speciality, int fightOrganised, int getCaught, int successRate) {  
         super(sname, gender, proll, beltcolor, tiecolor, rcount, fname, fightfought, toothbroken);  
         this.speciality = speciality;  
         this.fightOrganised = fightOrganised;  
         this.getCaught = getCaught;  
         this.successRate = successRate;  
     }  
     public String getSpeciality() {  
         return speciality;  
     }  
     public void setSpeciality(String speciality) {  
         this.speciality = speciality;  
     }  
     public int getFightOrganised() {  
         return fightOrganised;  
     }  
     public void setFightOrganised(int fightOrganised) {  
         this.fightOrganised = fightOrganised;  
     }  
     public int getGetCaught() {  
         return getCaught;  
     }  
     public void setGetCaught(int getCaught) {  
         this.getCaught = getCaught;  
     }  
     public int getSuccessRate() {  
         return successRate;  
     }  
     public void setSuccessRate(int successRate) {  
         this.successRate = successRate;  
     }  
 } 
17. Fighter.java
 package com.hibernate.inheritance;  
 /**  
  * Fighter domain class  
  * @author bkar  
  *  
  */  
 public class Fighter extends RedTagged {  
     private int punchCount;  
     private int kickCount;  
     private int beatenBrianiacCount;  
     private int beatenGeniusCount;  
     public Fighter(String sname, String gender, int proll, String beltcolor, String tiecolor, int rcount, String fname,  
             int fightfought, int toothbroken, int punchCount, int kickCount, int beatenBrianiacCount,  
             int beatedGeniusCount) {  
         super(sname, gender, proll, beltcolor, tiecolor, rcount, fname, fightfought, toothbroken);  
         this.punchCount = punchCount;  
         this.kickCount = kickCount;  
         this.beatenBrianiacCount = beatenBrianiacCount;  
         this.beatenGeniusCount = beatedGeniusCount;  
     }  
     public int getPunchCount() {  
         return punchCount;  
     }  
     public void setPunchCount(int punchCount) {  
         this.punchCount = punchCount;  
     }  
     public int getKickCount() {  
         return kickCount;  
     }  
     public void setKickCount(int kickCount) {  
         this.kickCount = kickCount;  
     }  
     public int getBeatenBrianiacCount() {  
         return beatenBrianiacCount;  
     }  
     public void setBeatenBrianiacCount(int beatenBrianiacCount) {  
         this.beatenBrianiacCount = beatenBrianiacCount;  
     }  
     public int getBeatenGeniusCount() {  
         return beatenGeniusCount;  
     }  
     public void setBeatenGeniusCount(int beatenGeniusCount) {  
         this.beatenGeniusCount = beatenGeniusCount;  
     }  
 } 
18. student1.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.inheritance">  
     <!-- Table Per Subclass Inheritance Mapping -->  
     <class name="Student" table="1student">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <joined-subclass name="HighSchool" table="1hschool">  
             <key column="sroll" />  
             <property name="hroll" column="hroll" type="int" />  
             <property name="game" column="game" type="string" />  
             <property name="hname" column="hname" type="string" />  
             <property name="arrestcount" column="acount" type="int" />  
         </joined-subclass>  
         <joined-subclass name="PrimarySchool" table="1pschool">  
             <key column="sroll" />  
             <property name="proll" column="proll" type="int" />  
             <property name="beltcolor" column="bcolor" type="string" />  
             <property name="tiecolor" column="tcolor" type="string" />  
             <joined-subclass name="YellowTagged" table="1ytagged">  
                 <key column="sroll" />  
                 <property name="yroll" column="yroll" type="int" />  
                 <property name="yname" column="yname" type="string" />  
                 <property name="bookworm" column="bworm" type="boolean" />  
                 <property name="iq" column="eyequeue" type="int" />  
                 <joined-subclass name="Genius" table="1genius">  
                     <key column="sroll" />  
                     <property name="getDumpedCount" column="gdcount" type="int" />  
                     <property name="crushCount" column="crush" type="int" />  
                     <property name="crashCount" column="crash" type="int" />  
                 </joined-subclass>  
                 <joined-subclass name="Brainiac" table="1brainiac">  
                     <key column="sroll" />  
                     <property name="readBookCount" column="rbcount" type="long" />  
                     <property name="yellowTeethCount" column="ytcount" type="int" />  
                     <property name="getBeatenByFightersCount" column="gbbf"  
                         type="long" />  
                 </joined-subclass>  
             </joined-subclass>  
             <joined-subclass name="RedTagged" table="1rtagged">  
                 <key column="sroll" />  
                 <property name="rcount" column="rcount" type="int" />  
                 <property name="fname" column="fname" type="string" />  
                 <property name="fightfought" column="ff" type="int" />  
                 <property name="toothbroken" column="tb" type="int" />  
                 <joined-subclass name="Wicked" table="1wicked">  
                     <key column="sroll" />  
                     <property name="speciality" column="spec" type="string" />  
                     <property name="fightOrganised" column="fo" type="int" />  
                     <property name="getCaught" column="caught" type="int" />  
                     <property name="successRate" column="sexrate" type="int" />  
                 </joined-subclass>  
                 <joined-subclass name="Fighter" table="1fight">  
                     <key column="sroll" />  
                     <property name="punchCount" column="punch" type="int" />  
                     <property name="kickCount" column="kick" type="int" />  
                     <property name="beatenBrianiacCount" column="bbc" type="int" />  
                     <property name="beatenGeniusCount" column="bgc" type="int" />  
                 </joined-subclass>  
             </joined-subclass>  
         </joined-subclass>  
     </class>  
 </hibernate-mapping> 
19. student2.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.inheritance">  
     <!-- Table Per Class Inheritance Mapping -->  
     <class name="Student" table="2student" discriminator-value="STU">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <discriminator column="stutype" />  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <subclass name="HighSchool" discriminator-value="HSTU">  
             <property name="hroll" column="hroll" type="int" />  
             <property name="game" column="game" type="string" />  
             <property name="hname" column="hname" type="string" />  
             <property name="arrestcount" column="acount" type="int" />  
         </subclass>  
         <subclass name="PrimarySchool" discriminator-value="PSTU">  
             <property name="proll" column="proll" type="int" />  
             <property name="beltcolor" column="bcolor" type="string" />  
             <property name="tiecolor" column="tcolor" type="string" />  
             <subclass name="YellowTagged" discriminator-value="YTAG">  
                 <property name="yroll" column="yroll" type="int" />  
                 <property name="yname" column="yname" type="string" />  
                 <property name="bookworm" column="bworm" type="boolean" />  
                 <property name="iq" column="eyequeue" type="int" />  
                 <subclass name="Genius" discriminator-value="GEN">  
                     <property name="getDumpedCount" column="gdcount" type="int" />  
                     <property name="crushCount" column="crush" type="int" />  
                     <property name="crashCount" column="crash" type="int" />  
                 </subclass>  
                 <subclass name="Brainiac" discriminator-value="BRA">  
                     <property name="readBookCount" column="rbcount" type="long" />  
                     <property name="yellowTeethCount" column="ytcount" type="int" />  
                     <property name="getBeatenByFightersCount" column="gbbf"  
                         type="long" />  
                 </subclass>  
             </subclass>  
             <subclass name="RedTagged" discriminator-value="RTAG">  
                 <property name="rcount" column="rcount" type="int" />  
                 <property name="fname" column="fname" type="string" />  
                 <property name="fightfought" column="ff" type="int" />  
                 <property name="toothbroken" column="tb" type="int" />  
                 <subclass name="Wicked" discriminator-value="WIC">  
                     <property name="speciality" column="spec" type="string" />  
                     <property name="fightOrganised" column="fo" type="int" />  
                     <property name="getCaught" column="caught" type="int" />  
                     <property name="successRate" column="sexrate" type="int" />  
                 </subclass>  
                 <subclass name="Fighter" discriminator-value="FIG">  
                     <property name="punchCount" column="punch" type="int" />  
                     <property name="kickCount" column="kick" type="int" />  
                     <property name="beatenBrianiacCount" column="bbc" type="int" />  
                     <property name="beatenGeniusCount" column="bgc" type="int" />  
                 </subclass>  
             </subclass>  
         </subclass>  
     </class>  
 </hibernate-mapping> 
20. studetn3.hbm.xml
 <?xml version="1.0" encoding="UTF-8"?>  
 <!DOCTYPE hibernate-mapping SYSTEM   
 "file:\\\D:\lib\hibernate-mapping-3.0.dtd">  
 <hibernate-mapping package="com.hibernate.inheritance">  
     <!-- Table Per Concrete Class Inheritance Mapping -->  
     <class name="Student" table="3student">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
     </class>  
     <class name="HighSchool" table="3hschool">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="hroll" column="hroll" type="int" />  
         <property name="game" column="game" type="string" />  
         <property name="hname" column="hname" type="string" />  
         <property name="arrestcount" column="acount" type="int" />  
     </class>  
     <class name="PrimarySchool" table="3pschool">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
     </class>  
     <class name="YellowTagged" table="3ytagged">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
         <property name="yroll" column="yroll" type="int" />  
         <property name="yname" column="yname" type="string" />  
         <property name="bookworm" column="bworm" type="boolean" />  
         <property name="iq" column="eyequeue" type="int" />  
     </class>  
     <class name="Genius" table="3genius">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
         <property name="yroll" column="yroll" type="int" />  
         <property name="yname" column="yname" type="string" />  
         <property name="bookworm" column="bworm" type="boolean" />  
         <property name="iq" column="eyequeue" type="int" />  
         <property name="getDumpedCount" column="gdcount" type="int" />  
         <property name="crushCount" column="crush" type="int" />  
         <property name="crashCount" column="crash" type="int" />  
     </class>  
     <class name="Brainiac" table="3brainiac">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
         <property name="yroll" column="yroll" type="int" />  
         <property name="yname" column="yname" type="string" />  
         <property name="bookworm" column="bworm" type="boolean" />  
         <property name="iq" column="eyequeue" type="int" />  
         <property name="readBookCount" column="rbcount" type="long" />  
         <property name="yellowTeethCount" column="ytcount" type="int" />  
         <property name="getBeatenByFightersCount" column="gbbf" type="long" />  
     </class>  
     <class name="RedTagged" table="3rtagged">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
         <property name="rcount" column="rcount" type="int" />  
         <property name="fname" column="fname" type="string" />  
         <property name="fightfought" column="ff" type="int" />  
         <property name="toothbroken" column="tb" type="int" />  
     </class>  
     <class name="Wicked" table="3wicked">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
         <property name="rcount" column="rcount" type="int" />  
         <property name="fname" column="fname" type="string" />  
         <property name="fightfought" column="ff" type="int" />  
         <property name="toothbroken" column="tb" type="int" />  
         <property name="speciality" column="spec" type="string" />  
         <property name="fightOrganised" column="fo" type="int" />  
         <property name="getCaught" column="caught" type="int" />  
         <property name="successRate" column="sexrate" type="int" />  
     </class>  
     <class name="Fighter" table="3fight">  
         <id name="sroll" column="sroll" type="int">  
             <generator class="increment" />  
         </id>  
         <property name="sname" column="sname" type="string" />  
         <property name="gender" column="gender" type="string" />  
         <property name="proll" column="proll" type="int" />  
         <property name="beltcolor" column="bcolor" type="string" />  
         <property name="tiecolor" column="tcolor" type="string" />  
         <property name="rcount" column="rcount" type="int" />  
         <property name="fname" column="fname" type="string" />  
         <property name="fightfought" column="ff" type="int" />  
         <property name="toothbroken" column="tb" type="int" />  
         <property name="punchCount" column="punch" type="int" />  
         <property name="kickCount" column="kick" type="int" />  
         <property name="beatenBrianiacCount" column="bbc" type="int" />  
         <property name="beatenGeniusCount" column="bgc" type="int" />  
     </class>  
 </hibernate-mapping> 
21. H01InheritanceInsert
 package com.client;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import org.hibernate.Transaction;  
 import com.hibernate.inheritance.Brainiac;  
 import com.hibernate.inheritance.Fighter;  
 import com.hibernate.inheritance.Genius;  
 import com.hibernate.inheritance.HighSchool;  
 import com.hibernate.inheritance.PrimarySchool;  
 import com.hibernate.inheritance.RedTagged;  
 import com.hibernate.inheritance.Student;  
 import com.hibernate.inheritance.Wicked;  
 import com.hibernate.inheritance.YellowTagged;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  * Client code to demonstrate persistence of domain classes having Inheritance using   
  *   
  * 1> Table Per Subclass Inheritance Mapping         - Add student1.hbm.xml in cfg file  
  * 2> Table Per Class Inheritance Mapping             - Add student2.hbm.xml in cfg file  
  * 3> Table Per Concrete Class Inheritance Mapping     - Add student3.hbm.xml in cfg file  
  * @author bkar  
  *  
  */  
 public class H01InheritanceInsert {  
     public static void main(String[] args) {  
         Transaction tx = null;  
         SessionFactory sf = null;  
         Session se = null;  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             se = sf.openSession();  
             tx = se.beginTransaction();  
             Student student = new Student("Nilu", "Female");  
             Integer in = (Integer) se.save(student);  
             System.out.println(in.intValue());  
             HighSchool hschool = new HighSchool("Pooja", "Female", 10003, "Chugalkhori", "chulbul", false, 9);  
             in = (Integer) se.save(hschool);  
             System.out.println(in.intValue());  
             PrimarySchool pschool = new PrimarySchool("Neelam", "Female", 1009, "rainbow", "lovly");  
             in = (Integer) se.save(pschool);  
             System.out.println(in.intValue());  
             YellowTagged ytagged = new YellowTagged("Nilesh", "Male", 2009, "micheal", "jackson", 1001, "DirtyDancer", true, 1390);  
             in = (Integer) se.save(ytagged);  
             System.out.println(in.intValue());  
             Genius genius = new Genius("Neeraj", "Male", 1300, "SkyBlue", "BabyPink", 666, "Mani", false, 1995, 17, 111, 110);  
             in = (Integer) se.save(genius);  
             System.out.println(in.intValue());  
             Brainiac brianey = new Brainiac("Moni", "Female", 9999, "Green", "Red", 1987, "BullsEye", true, 8888, 555999, 0, 1);  
             in = (Integer) se.save(brianey);  
             System.out.println(in.intValue());  
             RedTagged reddy = new RedTagged("Bajrang", "Male", 0007, "Red", "Red", 1100, "FireBall", 8998, 3);  
             in = (Integer) se.save(reddy);  
             System.out.println(in.intValue());  
             Wicked wicky = new Wicked("Preeti", "Female", 9000, "Black", "Black", 9099, "Killer", 99, 9, "Teasing", 999, 999, 100);  
             in = (Integer) se.save(wicky);  
             System.out.println(in.intValue());  
             Fighter fightnow = new Fighter("Nitu", "Female", 1111, "Purple", "Purple", 8888, "SoulSlayer", 9999, 1999, 555, 11008, 98, 99);  
             in = (Integer) se.save(fightnow);  
             System.out.println(in.intValue());  
             tx.commit();  
         } catch (Exception e) {  
             e.printStackTrace();  
             if (tx != null)  
                 tx.rollback();  
         } finally {  
             se.close();  
             sf.close();  
         }  
     }  
 } 
22. H01InheritanceSelect
 package com.client;  
 import java.util.List;  
 import org.hibernate.Query;  
 import org.hibernate.Session;  
 import org.hibernate.SessionFactory;  
 import com.hibernate.inheritance.Brainiac;  
 import com.hibernate.inheritance.Fighter;  
 import com.hibernate.inheritance.Genius;  
 import com.hibernate.inheritance.HighSchool;  
 import com.hibernate.inheritance.PrimarySchool;  
 import com.hibernate.inheritance.RedTagged;  
 import com.hibernate.inheritance.Student;  
 import com.hibernate.inheritance.Wicked;  
 import com.hibernate.inheritance.YellowTagged;  
 import com.hibernate.util.HibernateUtil;  
 /**  
  * Client code to retrieve data from domain classes having Inheritance.   
  * This select client works with all three types of Inheritance mapping.  
  * 1> Table Per Subclass Inheritance Mapping         - Add student1.hbm.xml in cfg file  
  * 2> Table Per Class Inheritance Mapping             - Add student2.hbm.xml in cfg file  
  * 3> Table Per Concrete Class Inheritance Mapping     - Add student3.hbm.xml in cfg file  
  * @author bkar  
  *  
  */  
 public class H01InheritanceSelect {  
     public static void main(String[] args) {  
         SessionFactory sf = null;  
         Session se = null;  
         try {  
             sf = HibernateUtil.getSessionFactory();  
             se = sf.openSession();  
             System.out.println("~~~~~~~~~~~~~~~~~~~Student~~~~~~~~~~~~~~~~~~~");  
             Query query = se.createQuery("FROM Student");  
             List stul = query.list();  
             for(Object obj:stul) {  
                 Student student = (Student)obj;  
                 System.out.println("Student :: SNAME : "+student.getSname()+", \t\t SROLL : "+student.getSroll()+", \t\t GENDER : "+student.getGender());  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~HighSchool~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM HighSchool");  
             stul = query.list();  
             for(Object obj:stul) {  
                 HighSchool highSchool = (HighSchool)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in HighSchool object from Student class :: "  
                 +" \t\t SNAME : "        +highSchool.getSname()  
                 +", \t\t SROLL : "        +highSchool.getSroll()  
                 +", \t\t GENDER : "        +highSchool.getGender());  
                 System.out.println("Properties in HighSchool object from HighSchool class :: "  
                 +" \t\t Hroll : "        +highSchool.getHroll()  
                 +", \t\t Hname : "        +highSchool.getHname()  
                 +", \t\t Game : "        +highSchool.getGame()  
                 +", \t\t InLove : "        +highSchool.isInlove()  
                 +", \t\t ArrestCount : "+highSchool.getArrestcount());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~PrimarySchool~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM PrimarySchool");  
             stul = query.list();  
             for(Object obj:stul) {  
                 PrimarySchool primarySchool = (PrimarySchool)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in PrimarySchool object from Student class :: "  
                 +" \t\t SNAME : "        +primarySchool.getSname()  
                 +", \t\t SROLL : "        +primarySchool.getSroll()  
                 +", \t\t GENDER : "        +primarySchool.getGender());  
                 System.out.println("Properties in PrimarySchool object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +primarySchool.getProll()  
                 +", \t\t BeltColor : "    +primarySchool.getBeltcolor()  
                 +", \t\t TieColor : "    +primarySchool.getTiecolor());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~YellowTagged~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM YellowTagged");  
             stul = query.list();  
             for(Object obj:stul) {  
                 YellowTagged yellowTagged = (YellowTagged)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in YellowTagged object from Student class :: "  
                 +" \t\t SNAME : "        +yellowTagged.getSname()  
                 +", \t\t SROLL : "        +yellowTagged.getSroll()  
                 +", \t\t GENDER : "        +yellowTagged.getGender());  
                 System.out.println("Properties in YellowTagged object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +yellowTagged.getProll()  
                 +", \t\t BeltColor : "    +yellowTagged.getBeltcolor()  
                 +", \t\t TieColor : "    +yellowTagged.getTiecolor());  
                 System.out.println("Properties in YellowTagged object from YellowTagged class :: "  
                 +" \t\t Yroll : "        +yellowTagged.getYroll()  
                 +", \t\t Yname : "        +yellowTagged.getYname()  
                 +", \t\t Bookworm : "    +yellowTagged.isBookworm()  
                 +", \t\t IQ : "            +yellowTagged.getIq());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~Genius~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM Genius");  
             stul = query.list();  
             for(Object obj:stul) {  
                 Genius genius = (Genius)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in Genius object from Student class :: "  
                 +" \t\t SNAME : "        +genius.getSname()  
                 +", \t\t SROLL : "        +genius.getSroll()  
                 +", \t\t GENDER : "        +genius.getGender());  
                 System.out.println("Properties in Genius object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +genius.getProll()  
                 +", \t\t BeltColor : "    +genius.getBeltcolor()  
                 +", \t\t TieColor : "    +genius.getTiecolor());  
                 System.out.println("Properties in YellowTagged object from Brainiac class :: "  
                 +" \t\t Yroll : "        +genius.getYroll()  
                 +", \t\t Yname : "        +genius.getYname()  
                 +", \t\t Bookworm : "    +genius.isBookworm()  
                 +", \t\t IQ : "            +genius.getIq());  
                 System.out.println("Properties in Genius object from Genius class :: "  
                 +" \t\t GetDumpedCount : "    +genius.getGetDumpedCount()  
                 +", \t\t CrushCount : "        +genius.getCrushCount()  
                 +", \t\t CrashCount : "        +genius.getCrashCount());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~Brainiac~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM Brainiac");  
             stul = query.list();  
             for(Object obj:stul) {  
                 Brainiac brainiac = (Brainiac)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in Brainiac object from Student class :: "  
                 +" \t\t SNAME : "        +brainiac.getSname()  
                 +", \t\t SROLL : "        +brainiac.getSroll()  
                 +", \t\t GENDER : "        +brainiac.getGender());  
                 System.out.println("Properties in Brainiac object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +brainiac.getProll()  
                 +", \t\t BeltColor : "    +brainiac.getBeltcolor()  
                 +", \t\t TieColor : "    +brainiac.getTiecolor());  
                 System.out.println("Properties in YellowTagged object from Brainiac class :: "  
                 +" \t\t Yroll : "        +brainiac.getYroll()  
                 +", \t\t Yname : "        +brainiac.getYname()  
                 +", \t\t Bookworm : "    +brainiac.isBookworm()  
                 +", \t\t IQ : "            +brainiac.getIq());  
                 System.out.println("Properties in Brainiac object from Brainiac class :: "  
                 +" \t\t ReadBookCount : "        +brainiac.getReadBookCount()  
                 +", \t\t YellowTeethCount : "    +brainiac.getYellowTeethCount()  
                 +", \t\t GetBeatenByFightersCount : "        +brainiac.getGetBeatenByFightersCount());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~RedTagged~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM RedTagged");  
             stul = query.list();  
             for(Object obj:stul) {  
                 RedTagged redtagged = (RedTagged)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in RedTagged object from Student class :: "  
                 +" \t\t SNAME : "        +redtagged.getSname()  
                 +", \t\t SROLL : "        +redtagged.getSroll()  
                 +", \t\t GENDER : "        +redtagged.getGender());  
                 System.out.println("Properties in RedTagged object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +redtagged.getProll()  
                 +", \t\t BeltColor : "    +redtagged.getBeltcolor()  
                 +", \t\t TieColor : "    +redtagged.getTiecolor());  
                 System.out.println("Properties in RedTagged object from RedTagged class :: "  
                 +" \t\t Rcount : "        +redtagged.getRcount()  
                 +", \t\t Fname : "        +redtagged.getFname()  
                 +", \t\t FightFought : "+redtagged.getFightfought()  
                 +", \t\t ToothBroken : "+redtagged.getToothbroken());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~Fighter~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM Fighter");  
             stul = query.list();  
             for(Object obj:stul) {  
                 Fighter fighter = (Fighter)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in Fighter object from Student class :: "  
                 +" \t\t SNAME : "        +fighter.getSname()  
                 +", \t\t SROLL : "        +fighter.getSroll()  
                 +", \t\t GENDER : "        +fighter.getGender());  
                 System.out.println("Properties in Fighter object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +fighter.getProll()  
                 +", \t\t BeltColor : "    +fighter.getBeltcolor()  
                 +", \t\t TieColor : "    +fighter.getTiecolor());  
                 System.out.println("Properties in Fighter object from RedTagged class :: "  
                 +" \t\t Rcount : "        +fighter.getRcount()  
                 +", \t\t Fname : "        +fighter.getFname()  
                 +", \t\t FightFought : "+fighter.getFightfought()  
                 +", \t\t ToothBroken : "+fighter.getToothbroken());  
                 System.out.println("Properties in Fighter object from Fighter class :: "  
                 +" \t\t PunchCount : "            +fighter.getPunchCount()  
                 +", \t\t KickCount : "            +fighter.getKickCount()  
                 +", \t\t BeatedGeniusCount : "    +fighter.getBeatenGeniusCount()  
                 +", \t\t BeatedBrainiacCount : "+fighter.getBeatenBrianiacCount());  
                 System.out.println("*******");  
             }  
             System.out.println("~~~~~~~~~~~~~~~~~~~Wicked~~~~~~~~~~~~~~~~~~~");  
             query = se.createQuery("FROM Wicked");  
             stul = query.list();  
             for(Object obj:stul) {  
                 Wicked wicked = (Wicked)obj;  
                 System.out.println("*******");  
                 System.out.println("Properties in Wicked object from Student class :: "  
                 +" \t\t SNAME : "        +wicked.getSname()  
                 +", \t\t SROLL : "        +wicked.getSroll()  
                 +", \t\t GENDER : "        +wicked.getGender());  
                 System.out.println("Properties in Wicked object from PrimarySchool class :: "  
                 +" \t\t Proll : "        +wicked.getProll()  
                 +", \t\t BeltColor : "    +wicked.getBeltcolor()  
                 +", \t\t TieColor : "    +wicked.getTiecolor());  
                 System.out.println("Properties in Wicked object from RedTagged class :: "  
                 +" \t\t Rcount : "        +wicked.getRcount()  
                 +", \t\t Fname : "        +wicked.getFname()  
                 +", \t\t FightFought : "+wicked.getFightfought()  
                 +", \t\t ToothBroken : "+wicked.getToothbroken());  
                 System.out.println("Properties in Wicked object from Wicked class :: "  
                 +" \t\t Speciality : "        +wicked.getSpeciality()  
                 +", \t\t FightOrganized : "    +wicked.getFightOrganised()  
                 +", \t\t GetCaught : "        +wicked.getGetCaught()  
                 +", \t\t SuccessRate : "    +wicked.getSuccessRate());  
                 System.out.println("*******");  
             }  
         } catch (Exception e) {  
             e.printStackTrace();  
         } finally {  
             se.close();  
             sf.close();  
         }  
     }  
 } 
Note
hibernate.cfg.xml includes the mapping resources in order which they written in configuration file.

If we write multiple mappings for one class in same hbm file. It will recognize the first mapping 
written in the file for that class.
If we want to execute the 2nd, then we have to comment out the first class block for that class. 
Commenting out 3rd is optional. As, commenting out 1st will make the 2nd as 1st, in the hbm file.
And, if we want to execute 3rd, then we have to comment 1st and 2nd both.

But, we can write separate mapping hbm files for same class, now the identification depends on the 
order of including the mapping resource in cfg file.
So, to execute specific one, make that mapping resource upper than the other mapping for 
the same class, in the cfg xml. 
Queries
delimiter $
select * from cust;
select * from students;
select * from courses;
select * from emails;
select * from marks;
select * from phones;
select * from refs;
$

delimiter $
select * from 1student;
select * from 1hschool;
select * from 1pschool;
select * from 1ytagged;
select * from 1genius;
select * from 1brainiac;
select * from 1rtagged;
select * from 1wicked;
select * from 1fight;
$
delimiter ;

delimiter $
select * from 3student;
select * from 3hschool;
select * from 3pschool;
select * from 3ytagged;
select * from 3genius;
select * from 3brainiac;
select * from 3rtagged;
select * from 3wicked;
select * from 3fight;
$
delimiter ;