Many-to-Many Hibernate Mapping with Example
In Many-to-Many association mapping, more than one objects of a persistent class are associated with more than one objects of another persistent class. For example, many (categories) are related to many (items) and vice-versa as it is a bidirectional mapping.
Here, we are creating an example of many-to-many association mapping. In this example, we need to specify @ManyToMany and @JoinTable annotations in the persistent class. Here, we need to take a collection property like Set, Map, Bag, and List in both persistent classes.
Following are the steps to create the example:
- Create all Persistent classes
In this step, we are going to develop persistent classes, i.e., Categories.java and Items.java.
Categories.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import java.util.Set; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="categories") public class Categories { @Id @Column(name="c_id") private int cate_id; @Column(name="c_name") private String cate_name; @ManyToMany(targetEntity=Items.class, cascade=CascadeType.ALL) @JoinTable(name="cate_items",joinColumns=@JoinColumn(name="c_id_fk",referencedColumnName="c_id"),inverseJoinColumns=@JoinColumn(name="item_id_fk",referencedColumnName="i_id")) private Set items; public int getCate_id() { return cate_id; } public void setCate_id(int cate_id) { this.cate_id = cate_id; } public String getCate_name() { public void setCate_id(int cate_id) { this.cate_id = cate_id; } public String getCate_name() { return cate_name; } public void setCate_name(String cate_name) { this.cate_name = cate_name; } public Set getItems() { return items; } public void setItems(Set items) { this.items = items; } } |
Items.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Entity @Table(name="items") public class Items { @Id @Column(name="i_id") private int item_id; @Column(name="i_name") private String item_name; @ManyToMany(targetEntity=Categories.class,mappedBy="items") private Set categories; public int getItem_id() { return item_id; } public void setItem_id(int item_id) { this.item_id = item_id; } public String getItem_name() { return item_name; } public void setItem_name(String item_name) { this.item_name = item_name; } public Set getCategories() { return categories; } public void setCategories(Set categories) { this.categories = categories; } } |
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 5.3//EN" "http://hibernate.sourceforge.net/hibernate-configuration-5.3.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <mapping class="com.app.Manytomany.Categories"/> <mapping class="com.app.Manytomany.Items"/> </session-factory> </hibernate-configuration> |
- 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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(); Categories cate1 = new Categories(); cate1.setCate_id(01); cate1.setCate_name("category 01"); Categories cate2 = new Categories(); cate2.setCate_id(02); cate2.setCate_name("category 02"); Items i= new Items(); i.setItem_id(31); i.setItem_name("item 11"); Items i2 =new Items(); i2.setItem_id(32); i2.setItem_name("item 21"); Set set= new HashSet(); set.add(i); set.add(i2); cate1.setItems(set); cate2.setItems(set); Transaction t= session.beginTransaction(); session.save(cate1); session.save(cate2); t.commit(); System.out.println("saved successfully!!!!"); session.close(); } } |
- Output

- Database tables
categories table

items table

cate_items table
