Hibernate One-to-Many Mapping

One-to-Many Hibernate Mapping Example

In One-to-many association mapping, only one object of a persistent class is related to many objects of another persistent class. It is a relationship in which one (parent) is related to many (children). It is a 1 to N relationship.

For example, one vendor can have many customers; therefore, we need to take a collection property such as Set, Map, and List in the parent class.

Now, we are going to create an example, in which two persistent classes are available, i.e., Vendor (parent) and Customer (child). Here, we need to define @OneToMany and @JoinColumn annotations in the persistent class.

Following are the steps to create the example:

  1. Create all persistent classes

In this step, we are going to develop persistent classes, i.e., Vendor.java and Customer.java.

Vendor.java

import java.util.Set;
 import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.Id;
 import javax.persistence.OneToMany;
 import javax.persistence.Table;
 @Entity
 @Table(name="vendor")
 public class Vendor {
  @Id
  @Column(name="vid")
  private int vendid;
  @Column(name="vname")
  private String name;
  @OneToMany(fetch=FetchType.LAZY,targetEntity=Customer.class,cascade=CascadeType.ALL)
  private Set children;
  public int getVendid() {
  return vendid;
  }
  public void setVendid(int vendid) {
  this.vendid = vendid;
  }
  public String getName() {
  return name;
  }
  public void setName(String name) {
  this.name = name;
  }
  public Set getChildren() {
  return children;
  }
  public void setChildren(Set children) {
  this.children = children;
  }
  } 

Customer.java

import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.Id;
 import javax.persistence.Table;
 @Entity
 @Table(name="customers")
 public class Customer {
  @Id
  @Column(name="cust_id")
  private int cid;
  @Column(name="cname")
  private String cname;
  public int getCid() {
  return cid;
  }
  public void setCid(int cid) {
  this.cid = cid;
  }
  public String getCname() {
  return cname;
  }
  public void setCname(String cname) {
  this.cname = cname;
  }
  } 
  • Create the configuration file

In this step, we are going to create the configuration class (hibernate.cfg.xml) which contains the information of persistent class and the database.

hibernate.cfg.xml

 
  
  
  
 update 
 org.hibernate.dialect.MySQL5Dialect
 com.mysql.jdbc.Driver
 jdbc:mysql://localhost:3306/hibernate
 root 
 root 
  
  
  
   
  • Create the main class that stores the object of the persistent class

In this step, we are going to create a class which consists of main() method, and stores the objects of persistent classes.

App.java

import java.util.HashSet;
 import java.util.Set;
 import org.hibernate.Session;
 import org.hibernate.SessionFactory;
 import org.hibernate.Transaction;
 import org.hibernate.cfg.Configuration;
 public class App 
 {
  public static void main( String[] args )
  {
  Configuration cfg= new Configuration();
  cfg.configure("hibernate.cfg.xml");
  SessionFactory factory= cfg.buildSessionFactory();
  Session session= factory.openSession();
  Vendor vendor= new Vendor();
  vendor.setVendid(01);
  vendor.setName("jyotika");
  Customer customer= new Customer();
  customer.setCid(10);
  customer.setCname("Shikha");
  Customer c= new Customer();
  c.setCid(16);
  c.setCname("shubham");
  Set s= new HashSet();
  s.add(customer);
  s.add(c);
  vendor.setChildren(s);
  Transaction t= session.beginTransaction();
  session.persist(vendor);
  t.commit();
  session.close();
  System.out.println("saved successfully");
  factory.close();
  }
 } 
  • Output
One-to-Many Hibernate Mapping
  • Tables

Vendor

Vendor One-to-Many Hibernate Mapping

      Customers

Customers One-to-Many Hibernate Mapping

vendor_customers

vendor_customers One-to-Many Hibernate Mapping



ADVERTISEMENT
ADVERTISEMENT