Hibernate Many to One Mapping using XML

In numerous to one mapping, different credits can be alluded to one trait as it were.

In this precedent, each worker has one organization address just and one location has a place with numerous representatives. Here, we will perform numerous to one mapping utilizing XML.

1) Persistent classes for balanced mapping

There are two tenacious classes Employee.java and Address.java. Representative class contains Address class reference and bad habit versa.

Employee.java

  1. package com.javaocodegeeks;    
  2.     
  3. public class Employee {    
  4. private int employeeId;    
  5. private String name,email;    
  6. private Address address;    
  7. //setters and getters    
  8. }    

Address.java

  1. package com.javaocodegeeks;    
  2.     
  3. public class Address {    
  4. private int addressId;    
  5. private String addressLine1,city,state,country;    
  6. private int pincode;    
  7. private Employee employee;    
  8. //setters and getters    
  9. }  

2) Mapping files for the persistent classes

The two mapping files are employee.hbm.xml and address.hbm.xml.

employee.hbm.xml

  1. <?xml version='1.0' encoding='UTF-8'?>    
  2. <!DOCTYPE hibernate-mapping PUBLIC    
  3.           "-//Hibernate/Hibernate Mapping DTD 5.3//EN"    
  4.           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">    
  5.     
  6.           <hibernate-mapping>    
  7.           <class name="com.javaocodegeeks.Employee" table="emp22">    
  8.           <id name="employeeId">    
  9.           <generator class="increment"></generator>    
  10.           </id>    
  11.           <property name="name"></property>    
  12.           <property name="email"></property>    
  13.               
  14.           <many-to-one name="address" cascade="all"></many-to-one>    
  15.           </class>    
  16.               
  17.           </hibernate-mapping>   

address.hbm.xml

<?xml version='1.0' encoding='UTF-8'?>    
  • <!DOCTYPE hibernate-mapping PUBLIC    
  •           "-//Hibernate/Hibernate Mapping DTD 5.3//EN"    
  •           "http://hibernate.sourceforge.net/hibernate-mapping-5.3.dtd">    
  •     
  •           <hibernate-mapping>    
  •           <class name="com.javaocodegeeks.Address" table="address22">    
  •           <id name="addressId">    
  •           <generator class="increment"></generator>    
  •           </id>    
  •           <property name="addressLine1"></property>    
  •           <property name="city"></property>    
  •           <property name="state"></property>    
  •           <property name="country"></property>    
  •           <property name="pincode"></property>    
  •           </class>    
  •           </hibernate-mapping>    
  • 3) Configuration file

    This file contains information about the database and mapping file.

    1. <?xml version='1.0' encoding='UTF-8'?>    
    2. <!DOCTYPE hibernate-configuration PUBLIC    
    3.           "-//Hibernate/Hibernate Configuration DTD 5.3//EN"    
    4.           "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd">    
    5.      
    6. <hibernate-configuration>    
    7.     
    8.     <session-factory>    
    9.         <property name="hbm2ddl.auto">update</property>    
    10.         <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>    
    11.         <property name="connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>    
    12.         <property name="connection.username">system</property>    
    13.         <property name="connection.password">jtp</property>    
    14.         <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>    
    15.     <mapping resource="employee.hbm.xml"/>    
    16.     <mapping resource="address.hbm.xml"/>    
    17.     </session-factory>    
    18.     
    19. </hibernate-configuration>    

    4) User classes to store and fetch the data

    Store.java

    1. package com.javaocodegeeks;    
    2.   
    3. import org.hibernate.*;  
    4. import org.hibernate.boot.Metadata;  
    5. import org.hibernate.boot.MetadataSources;  
    6. import org.hibernate.boot.registry.StandardServiceRegistry;  
    7. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;    
    8.     
    9. public class Store {    
    10. public static void main(String[] args) {    
    11.       
    12.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
    13.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
    14.       
    15.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
    16.     Session session=factory.openSession();  
    17.       
    18.     Transaction t=session.beginTransaction();    
    19.         
    20.     Employee e1=new Employee();    
    21.     e1.setName("smote Malik");    
    22.     e1.setEmail("smote@gmail.com");    
    23.       
    24.     Employee e2=new Employee();  
    25.     e2.setName("renna Verma");  
    26.     e2.setEmail("renna@gmail.com");  
    27.         
    28.     Address address1=new Address();    
    29.     address1.setAddressLine1("G-13,Sector 3");    
    30.     address1.setCity("Noida");    
    31.     address1.setState("UP");    
    32.     address1.setCountry("India");    
    33.     address1.setPincode(201301);    
    34.         
    35.     e1.setAddress(address1);    
    36.     e2.setAddress(address1);  
    37.   
    38.     session.persist(e1);    
    39.     session.persist(e2);  
    40.     t.commit();    
    41.         
    42.     session.close();    
    43.     System.out.println("success");    
    44. }    
    45. }     import org.hibernate.*; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; import org.hibernate.boot.registry.StandardServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class Store { public static void main(String[] args) { StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build(); Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build(); SessionFactory factory=meta.getSessionFactoryBuilder().build(); Session session=factory.openSession(); Transaction t=session.beginTransaction(); Employee e1=new Employee(); e1.setName("Smite"); e1.setEmail("smote@gmail.com"); Employee e2=new Employee(); e2.setName("Renna sky"); e2.setEmail("renna@gmail.com"); Address address1=new Address(); address1.setAddressLine1("Statue of Liberty National Monument"); address1.setCity("New York"); address1.setState("NY "); address1.setCountry("USA"); address1.setPincode(10004); e1.setAddress(address1); e2.setAddress(address1); session.persist(e1); session.persist(e2); t.commit(); session.close(); System.out.println("success"); } }

    Fetch.java

    1. package com.javaocodegeeks;    
    2. import java.util.Iterator;    
    3. import java.util.List;  
    4.   
    5. import javax.persistence.TypedQuery;    
    6. import org.hibernate.Session;    
    7. import org.hibernate.SessionFactory;  
    8. import org.hibernate.boot.Metadata;  
    9. import org.hibernate.boot.MetadataSources;  
    10. import org.hibernate.boot.registry.StandardServiceRegistry;  
    11. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;   
    12.     
    13. public class Fetch {    
    14. public static void main(String[] args) {    
    15.     StandardServiceRegistry ssr=new StandardServiceRegistryBuilder().configure("hibernate.cfg.xml").build();  
    16.     Metadata meta=new MetadataSources(ssr).getMetadataBuilder().build();  
    17.       
    18.     SessionFactory factory=meta.getSessionFactoryBuilder().build();  
    19.     Session session=factory.openSession();  
    20.         
    21.     TypedQuery query=session.createQuery("from Employee e");    
    22.     List<Employee> list=query.getResultList();   
    23.         
    24.     Iterator<Employee> itr=list.iterator();    
    25.     while(itr.hasNext()){    
    26.      Employee emp=itr.next();    
    27.      System.out.println(emp.getEmployeeId()+" "+emp.getName()+" "+emp.getEmail());    
    28.      Address address=emp.getAddress();    
    29.      System.out.println(address.getAddressLine1()+" "+address.getCity()+" "+    
    30.         address.getState()+" "+address.getCountry()+" "+address.getPincode());    
    31.     }    
    32.     
    33.     session.close();    
    34.     System.out.println("success");    
    35. }    
    36. }    





    javacodegeeks is optimized for learning.© javacodegeeks .
    All Right Reserved and you agree to have read and accepted our term and condition