<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">
<div dir="ltr" style="text-align: justify;" trbidi="on">Many to many mapping in hibernate is required when each record in an entity may have many linked records in another entity and vice-versa.</p>
<div align='center' id="ads-id"></div>
<p>
 In this tutorial you will learn how to map <b>many-to-many</b> relationship using Hibernate. Consider the following relationship between <i>Vehicle </i>and <i>UserDetails</i><i style="color: #20124d;"></i> entity.</p>
<p>  ; </p>
<div class="separator" style="clear: both; text-align: center;"><img border="0" src="https://i1.wp.com/dineshonjava.com/wp-content/uploads/2017/04/many-to-many.jpg" /></div>
<p></p>
<div style="color: #20124d;">According to the relationship a user can have in any number of vehicles and the vehicle can have any number of users.</p>
<p><b>UserDetail.java</b></p>
<pre class="highlight" name="code">package com.sdnext.hibernate.tutorial.dto; 
 
import java.util.ArrayList; 
import java.util.Collection; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.ManyToMany; 
import javax.persistence.Table; 
 
<b>@Entity 
@Table (name="USER")</b> 
public class UserDetails 
{ 
 ; ; ; <b>@Id 
 ; ; ; @Column(name="USER_ID") 
 ; ; ; @GeneratedValue(strategy=GenerationType.AUTO)</b> 
 ; ; ; private int ; ; ; userId; 
 ; ; ; 
 ; ; ; <b>@Column(name="USER_NAME") </b> 
 ; ; ; private String userName; 
 ; ; ; 
 ; ; ; <b>@ManyToMany</b> 
 ; ; ; private Collection<;Vehicle>; vehicle = new ArrayList<;Vehicle>;(); 
 ; ; ; 
 ; ; ; public int getUserId() { 
 ; ; ;  ; ; ; return userId; 
 ; ; ; } 
 ; ; ; public Collection<;Vehicle>; getVehicle() { 
 ; ; ;  ; ; ; return vehicle; 
 ; ; ; } 
 ; ; ; public void setVehicle(Collection<;Vehicle>; vehicle) { 
 ; ; ;  ; ; ; this.vehicle = vehicle; 
 ; ; ; } 
 ; ; ; public void setUserId(int userId) { 
 ; ; ;  ; ; ; this.userId = userId; 
 ; ; ; } 
 ; ; ; public String getUserName() { 
 ; ; ;  ; ; ; return userName; 
 ; ; ; } 
 ; ; ; public void setUserName(String userName) { 
 ; ; ;  ; ; ; this.userName = userName; 
 ; ; ; } 
}</pre>
</div>
</div>
<p>
<b>Vehicle.java</b></p>
<p></p>
<pre class="highlight" name="code">package com.sdnext.hibernate.tutorial.dto; 
 
import java.util.ArrayList; 
import java.util.Collection; 
 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.ManyToMany; 
import javax.persistence.Table; 
 
<b>@Entity 
@Table(name="VEHICLE")</b> 
public class Vehicle 
{<b> 
 ; ; ; @Id 
 ; ; ; @GeneratedValue(strategy=GenerationType.AUTO) 
 ; ; ; @Column(name="VEHICLE_ID")</b> 
 ; ; ; private int vehicleId; 
 ; ; ; 
 ; ; ; <b>@Column(name="VEHICLE_NAME")</b> 
 ; ; ; private String vehicleName; 
 ; ; ; 
 ; ; ; <b>@ManyToMany(mappedBy="vehicle")</b> 
 ; ; ; private Collection<;UserDetails>; user = new ArrayList<;UserDetails>;(); 
 ; ; ; 
 ; ; ; public Collection<;UserDetails>; getUser() { 
 ; ; ;  ; ; ; return user; 
 ; ; ; } 
 ; ; ; public void setUser(Collection<;UserDetails>; user) { 
 ; ; ;  ; ; ; this.user = user; 
 ; ; ; } 
 ; ; ; public int getVehicleId() { 
 ; ; ;  ; ; ; return vehicleId; 
 ; ; ; } 
 ; ; ; public void setVehicleId(int vehicleId) { 
 ; ; ;  ; ; ; this.vehicleId = vehicleId; 
 ; ; ; } 
 ; ; ; public String getVehicleName() { 
 ; ; ;  ; ; ; return vehicleName; 
 ; ; ; } 
 ; ; ; public void setVehicleName(String vehicleName) { 
 ; ; ;  ; ; ; this.vehicleName = vehicleName; 
 ; ; ; } 
}</pre>
</div>
<p>
<b style="color: purple;">hibernate.cfg.xml</b>:</p>
<p></p>
<pre class="highlight" name="code"><;hibernate-configuration>; 
 <;session-factory>; 
 <;!-- Database connection settings -->; 
 <;property name="connection.driver_class">;com.mysql.jdbc.Driver<;/property>; 
 <;property name="connection.url">;jdbc:mysql://localhost:3306/hibernateDB<;/property>; 
 <;property name="connection.username">;root<;/property>; 
 <;property name="connection.password">;root<;/property>; 
 
 <;!-- JDBC connection pool (use the built-in) -->; 
 <;property name="connection.pool_size">;1<;/property>; 
 
 <;!-- SQL dialect -->; 
 <;property name="dialect">;org.hibernate.dialect.MySQLDialect<;/property>; 
 
 <;!-- Enable Hibernate's automatic session context management -->; 
 <;property name="current_session_context_class">;thread<;/property>; 
 
 <;!-- Disable the second-level cache -->; 
 <;property name="cache.provider_class">;org.hibernate.cache.NoCacheProvider<;/property>; 
 
 <;!-- Echo all executed SQL to stdout -->; 
 <;property name="show_sql">;true<;/property>; 
 
 <;!-- Drop and re-create the database schema on startup -->; 
 <;property name="hbm2ddl.auto">;create<;/property>; 
 
 
 <;mapping class="com.sdnext.hibernate.tutorial.dto.UserDetails">; 
 <;mapping class="com.sdnext.hibernate.tutorial.dto.Vehicle">; 
 
 <;/mapping>;<;/mapping>;<;/session-factory>; 
 <;/hibernate-configuration>; 
</pre>
</div>
<p><b>HibernateTestDemo.java</b></p>
<pre class="highlight" name="code"><b>package com.sdnext.hibernate.tutorial; 
 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.AnnotationConfiguration; 
 
import com.sdnext.hibernate.tutorial.dto.UserDetails; 
import com.sdnext.hibernate.tutorial.dto.Vehicle; 
 
 
public class HibernateTestDemo { 
 
 ; ;  ;/** 
 ; ;  ; * @param args 
 ; ;  ; */ 
 ; ;  ;public static void main(String[] args) 
 ; ;  ;{ 
 ; ;  ; ; ; ; UserDetails user = new UserDetails(); 
 ; ;  ; ; ; ; UserDetails user2 = new UserDetails(); 
 ; ;  ; ; ; ; 
 ; ;  ; ; ; ; Vehicle vehicle = new Vehicle(); 
 ; ;  ; ; ; ; Vehicle vehicle2 = new Vehicle(); 
 ; ;  ; ; ; ; 
 ; ;  ; ; ; ; vehicle.setVehicleName("Car"); 
 ; ;  ; ; ; ; vehicle.getUser().add(user); 
 ; ;  ; ; ; ; vehicle.getUser().add(user2); 
 ; ;  ; ; ; ; 
 ; ;  ; ; ; ; vehicle2.setVehicleName("Jeep"); 
 ; ;  ; ; ; ; vehicle2.getUser().add(user2); 
 ; ;  ; ; ; ; vehicle2.getUser().add(user); 
 ; ;  ; ; ; ; 
 ; ;  ; ; ; ; user.setUserName("First User"); 
 ; ;  ; ; ; ; user2.setUserName("Second User"); 
 ; ;  ; ; ; ; user.getVehicle().add(vehicle); 
 ; ;  ; ; ; ; user.getVehicle().add(vehicle2); 
 ; ;  ; ; ; ; user2.getVehicle().add(vehicle); 
 ; ;  ; ; ; ; user2.getVehicle().add(vehicle2); 
 ; ;  ; ; ; ; 
 ; ;  ; ; ; ; SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 
 ; ;  ; ; ; ; Session session = sessionFactory.openSession(); 
 ; ;  ; ; ; ; session.beginTransaction(); 
 ; ;  ; ; ; ; session.save(vehicle); 
 ; ;  ; ; ; ; session.save(vehicle2); 
 ; ;  ; ; ; ; session.save(user); 
 ; ;  ; ; ; ; session.save(user2); 
 ; ;  ; ; ; ; session.getTransaction().commit(); 
 ; ;  ; ; ; ; session.close(); 
 ; ; ; } 
}</b></pre>
<p><b><br />
</b><b></b> <br />
<b>Using Mapping files instead of Annotation</b><b></b> <br />
<b><br />
</b> <b>userDetail.hbm.xml</b></p>
<pre class="highlight" name="code"><;hibernate-mapping>; 
 <;class name="com.sdnext.hibernate.tutorial.dto.UserDetails" table="USER">; 
 <;id column="ID" name="userId" type="long">; 
 <;generator class="assigned">; 
 <;/generator>;<;/id>; 
 <;property column="column" name="UserName">; 
 <;list cascade="all" name="vehicle" table="USER_VEHICLE">; 
 <;key column="USER_ID">; 
 <;many-to-many class="com.sdnext.hibernate.tutorial.dto.Vehicle" column="VEHICLE_ID">; 
 <;/many-to-many>;<;/key>;<;/list>; 
 <;/property>;<;/class>; 
<;/hibernate-mapping>; 
</pre>
<p><b>vehicle.hbm.xml</b></p>
<pre class="highlight" name="code"><;hibernate-mapping>; 
 <;class name="com.sdnext.hibernate.tutorial.dto.Vehicle" table="VEHICLE">; 
 <;id column="ID" name="userId" type="long">; 
 <;generator class="assigned">; 
 <;/generator>;<;/id>; 
 <;property column="VEHICLE_NAME" name="vehicleName">; 
 <;/property>;<;/class>; 
<;/hibernate-mapping>;</pre>
<p></p>
<div style="color: purple;">
<div style="color: purple;"><b>In <a href="https://dineshonjava.com/p/cascadetypes-and-other-things.html" style="color: #20124d;">Next Chapter</a> we will discuss about the <a href="https://dineshonjava.com/p/cascadetypes-and-other-things.html">Cascade Type.</a></b><br />
<b><br />
</b> <b> ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ;<;<;<a href="https://dineshonjava.com/p/hibernate-many-to-one-mapping-tutorial.html" style="font-family: Times, 'Times New Roman', serif;">Previous Chapter 19</a><;<;  ; ; >;>;N<a href="https://dineshonjava.com/p/cascadetypes-and-other-things.html" style="font-family: Times, 'Times New Roman', serif;">ext Chapter21>;>;</a></b><br />
<b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b> <b><br />
</b> </div>
</div>
</div>

View Comments
can you plz tell the table structure of manytomany mapping
Hi Heena,
Please go to the following link and you can check table structure as well.
https://dineshonjava.com/spring-crud-example-using-many-to-one/
https://dineshonjava.com/spring-crud-example-using-one-to-one/