Many to Many Hibernate/JPA Example

In this article we will learn how to create a sample JPA application which uses a Many To Many Relationship between Entity objects. Then, we will deploy and test the application on WildFly.

Many to Many overview

When one or more Entity are associated with a Collection of other Entity, and the Entity also have
overlapping associations with the same target Entity, we must model this relation as many-to-many. Each Entity will have a collection-valued association that contains entities of the target Entity type.

For example, in the following association, a Customer has Many Address and an Address can, in turn, have Many Customer:

Hibernate Many To Many

Let’s see how to code this relationship with 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");

		Customer customer2 = new Customer();	 
		customer2.setName("Frank Smith");

		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: