Hibernate Many to One Mapping using Annotation

In many to one mapping, various attributes can be referred to one attribute only.

In this example, every employee has one company address only and one address belongs to many employees. Here, we are going to perform many to one mapping using annotation.

Let's look at the persistent classes

1) Persistent classes for one to one mapping

There are two persistent classes Employee.java and Address.java. Employee class contains Address class reference and vice versa.

Employee.java

Employee.java

  1. package com.javacodegeeks;  
  2. import javax.persistence.*;  
  3.   
  4. @Entity  
  5. @Table(name="emp107")  
  6. public class Employee {    
  7.      @Id  
  8.      @GeneratedValue(strategy=GenerationType.AUTO)    
  9. private int employeeId;    
  10. private String name,email;    
  11. @ManyToOne(cascade=CascadeType.ALL)  
  12. private Address address;  
  13. public int getEmployeeId() {  
  14.     return employeeId;  
  15. }  
  16. public void setEmployeeId(int employeeId) {  
  17.     this.employeeId = employeeId;  
  18. }  
  19. public String getName() {  
  20.     return name;  
  21. }  
  22. public void setName(String name) {  
  23.     this.name = name;  
  24. }  
  25. public String getEmail() {  
  26.     return email;  
  27. }  
  28. public void setEmail(String email) {  
  29.     this.email = email;  
  30. }  
  31. public Address getAddress() {  
  32.     return address;  
  33. }  
  34. public void setAddress(Address address) {  
  35.     this.address = address;  
  36. }    
  37. }  

Address.java

  1. package com.javacodegeeks;  
  2.   
  3. import javax.persistence.*;  
  4.   
  5. @Entity  
  6. @Table(name="address107")  
  7. public class Address {  
  8.     @Id  
  9.     @GeneratedValue(strategy=GenerationType.AUTO)  
  10.     private int addressId;    
  11.     private String addressLine1,city,state,country;    
  12.     private int pincode;    
  13.    @OneToOne(cascade=CascadeType.ALL)  
  14.     private Employee employee;  
  15.     public int getAddressId() {  
  16.         return addressId;  
  17.     }  
  18.     public void setAddressId(int addressId) {  
  19.         this.addressId = addressId;  
  20.     }  
  21.     public String getAddressLine1() {  
  22.         return addressLine1;  
  23.     }  
  24.     public void setAddressLine1(String addressLine1) {  
  25.         this.addressLine1 = addressLine1;  
  26.     }  
  27.     public String getCity() {  
  28.         return city;  
  29.     }  
  30.     public void setCity(String city) {  
  31.         this.city = city;  
  32.     }  
  33.     public String getState() {  
  34.         return state;  
  35.     }  
  36.     public void setState(String state) {  
  37.         this.state = state;  
  38.     }  
  39.     public String getCountry() {  
  40.         return country;  
  41.     }  
  42.     public void setCountry(String country) {  
  43.         this.country = country;  
  44.     }  
  45.     public int getPincode() {  
  46.         return pincode;  
  47.     }  
  48.     public void setPincode(int pincode) {  
  49.         this.pincode = pincode;  
  50.     }  
  51.     public Employee getEmployee() {  
  52.         return employee;  
  53.     }  
  54.     public void setEmployee(Employee employee) {  
  55.         this.employee = employee;  
  56.     }    
  57. }  

2) Add project information and configuration in pom.xml file.

Open pom.xml file and click source. Now, add the below dependencies between <dependencies>....</dependencies> tag. These dependencies are used to add the jar files in Maven project.

  1. <dependency>    
  2.     <groupId>org.hibernate</groupId>    
  3.     <artifactId>hibernate-core</artifactId>    
  4.     <version>5.3.1.Final</version>    
  5. </dependency>    
  1. <dependency>    
  2.     <groupId>com.oracle</groupId>    
  3.     <artifactId>ojdbc14</artifactId>    
  4.     <version>10.2.0.4.0</version>    
  5. </dependency>    

3) Configuration file

This file contains information about the database and mapping file.

hibernate.cfg.xml

  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">create</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 class="com.javacodegeeks.Address"/>    
  16.     <mapping class="com.javacodegeeks.Employee"/>    
  17.     </session-factory>    
  18.     
  19. </hibernate-configuration>    

4) User classes to store and fetch the data

Store.java

  1. package com.javacodegeeks;    
  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("Ravi Malik");    
  22.     e1.setEmail("ravi@gmail.com");    
  23.       
  24.     Employee e2=new Employee();  
  25.     e2.setName("Anuj Verma");  
  26.     e2.setEmail("anuj@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. }    





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