Many to Many Hibernate/JPA Example

In Java Persistence API (JPA), the Many-to-Many relationship represents a common scenario where multiple instances of one entity are associated with multiple instances of another entity. This tutorial will guide you through the process of implementing a Many-to-Many relationship using JPA.

What is a Many to Many Relationship?

In a Many-to-Many relationship, entities from both sides of the relationship can be related to multiple instances of the other side. For example, in the following association, a Customer has Many Address and an Address can, in turn, have Many Customer:

Hibernate Many To Many

To understand the implications of the Many-to-Many relationship on entity mapping and database schema we will see a practical example using a JPA/Hibernate application.

Coding a sample application

A many-to-many mapping is expressed on both the source and target entities as a @ManyToMany
annotation on the collection attributes. For example, in the following code, the Customer Entity has an addresses attribute that has been annotated with @ManyToMany. Likewise, the Address Entity has a customers attribute that has also been annotated with @ManyToMany:


public class Customer implements Serializable {

    private Long id;
    private String name;
    private String email;
    @Column(name = "phone_number")
    private String phoneNumber;

    @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    private List<Address> addresses = new ArrayList();

    // Getters/Setters omitted for brevity

Conversely, this is the Address Entity:

public class Address implements Serializable {
   private Long id;
   private String street;

   private String city;
   @ManyToMany(mappedBy = "addresses")
   private List<Customer> customers = new ArrayList();


Finally, we will add a simple Stateless Bean to insert some records and fetch a Customer with the related Addresses:


public class ServiceBean {

	private EntityManager em;

	public String create() {
		Customer customer1 = new Customer();	 
		customer1.setName("John Smith");
		customer1.setEmail("[email protected]");

		Customer customer2 = new Customer();	 
		customer2.setName("Frank Smith");
		customer2.setEmail("[email protected]");

		Address address1 = new Address();
		address1.setStreet("15th Avenue");
		address1.setCity("New York");
		Address address2 = new Address();
		address2.setStreet("Rue de Rivoli");
		List<Address> list1 = new ArrayList();

		List<Address> list2 = new ArrayList();


		return "Created!";

	public Customer findCustomer(Long i) {
		Customer c = this.em.find(Customer.class, i);
		System.out.println("Found Customer "+c);
		System.out.println("Found Addresses "+c.getAddresses());
		return c;



To run the above example using the default H2 Datasource, you can include in your persistence.xml the following persistence unit:

    <persistence-unit name="primary">
            <!-- Properties for Hibernate -->
            <property name="" value="create-drop" />
            <property name="hibernate.show_sql" value="true" />


In this example we have learnt how to code a many-to-many relation with Hibernate/JPA and added a simple Bean to test it.

Source code for this example: