Hibernate Self Join Many to Many Mapping using Annotation

In this tutorial I will show you how to implement Self-Join Many to Many mapping using Hibernate Annotation.

Figure 12.1 illustrates Self Reference many-to-many relationship.

Figure 12.1 Figure 12.1

According to this relationship each person can have friends and each friend is a person.

First create a new Java Project and configure it as Maven Project. For Reference, Click Here

Add the following dependencies in pom.xml

<dependencies>
 
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
 
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.15</version>
    </dependency>
 
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.6.3.Final</version>
    </dependency>
 
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.6</version>
    </dependency>
</dependencies>

1. Sql Script

Use the following Sql Script for creating table.


create table Person(
    person_id int(10) primary key NOT NULL AUTO_INCREMENT,
    Name varchar(50));
 
create table friends(
    person_id int(10) NOT NULL REFERENCES person (person_id),
    friend_id int(10) NOT NULL REFERENCES person (person_id),
    PRIMARY KEY (person_id, friend_id)
    );

2. Pojo

Now create Person Class as following.

Person.java

package com.kruders.model.bean;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.JoinColumn;

@Entity
@Table(name="PERSON")
public class Person {
	
	@Id
	@Column(name="PERSON_ID")
	@GeneratedValue
	private Integer personId;
	
	@Column(name="NAME")
	private String name;
	
	
	
	@ManyToMany(cascade={CascadeType.ALL})
    @JoinTable(name="FRIENDS",
                joinColumns={@JoinColumn(name="PERSON_ID")},
                inverseJoinColumns={@JoinColumn(name="FRIEND_ID")})
    private Set<Person> persons = new HashSet<Person>();

	@ManyToMany(mappedBy="persons")
	private Set<Person> friends = new HashSet<Person>();

	public Person() {
	}

	public Person(String name) {
		this.name = name;
	}

	public Set<Person> getFriends() {
		return friends;
	}

	public void setFriends(Set<Person> friends) {
		this.friends = friends;
	}

	public Set<Person> getPersons() {
		return persons;
	}

	public void setPersons(Set<Person> persons) {
		this.persons = persons;
	}

	public Integer getPersonId() {
		return personId;
	}

	public void setPersonId(Integer personId) {
		this.personId = personId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	

}

We have defined two attributes Set<Person> persons and Set<Person> friends. The persons attribute is relationship owner. @ManyToMany annotation is defined on both persons and friends attributes.

3. Hibernate Configuration File

Now create the hibernate configuration file and add all the mapping files.

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
	<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
	<property name="hibernate.connection.username">root</property>
	<property name="hibernate.connection.password"></property>
	<property name="connection.pool_size">1</property>
	<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	<property name="show_sql">true</property>
 	<mapping class="com.kruders.model.bean.Person" />
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

Now create HibernateUtil class. The HibernateUtil class helps in creating the SessionFactory from the Hibernate configuration file. A org.hibernate.SessionFactory is used to obtain org.hibernate.Session instances. A org.hibernate.Session represents a single-threaded unit of work. The org.hibernate.SessionFactory is a thread-safe global object that is instantiated once.

package com.kruders.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
  
public class HibernateUtil {
  
    private static final SessionFactory sessionFactory = buildSessionFactory();
  
    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new Configuration().configure().buildSessionFactory();
        } catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }
  
    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

5. Run Program

Create Main.java class that perform CRUD operations and run it as Java Application

package com.kruders.core;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.kruders.model.bean.Person;
import com.kruders.util.HibernateUtil;


public class Main {
	public static void main(String args[]) {
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction transaction = null;
		try {
			
			transaction = session.beginTransaction();
			
	        Person person1 = new Person("Puneet");
	        Person person2 = new Person("Rahul");
	        Person person3 = new Person("Shally");
	        
	        person1.getPersons().add(person2);
	        session.save(person1);
	        session.save(person2);
	        session.save(person3);
	       
			transaction.commit();
		}catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		
		//listEmployee();
		//updateEmployee("Puneet", "Raghav");
		//listEmployee();
		//deleteEmployee(5);
	}
	
	public void listEmployee() {
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            List persons = session.createQuery("from Person").list();
            for (Iterator iterator = persons.iterator(); iterator.hasNext();) {
                Person person = (Person) iterator.next();
                System.out.println("Name " + person.getName());
               
            }
            transaction.commit();
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
	
	public void updateEmployee(String oldName, String newName) {
		Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            List persons = session.createQuery("from Person where Name ='" + oldName + "'").list();
            for (Iterator iterator = persons.iterator(); iterator.hasNext();) {
            	Person person = (Person) iterator.next();
            	person.setName(newName);
            }
            transaction.commit();
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
	     
    public void deleteEmployee(Integer employeeId) {
    	Session session = HibernateUtil.getSessionFactory().openSession();
	    Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            List<Person> persons = session.createQuery("from Person where personId ='" + employeeId + "'").list();
            for(Person person : persons) {
            	System.out.println("Name " + person.getName());
            	session.delete(person);
            }
           
            transaction.commit();
        } catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}

The folder structure of the example is shown below in Figure 12.2

Figure 12.2 Figure 12.2

You can download the source code of this example here.

No comments yet.

Leave a Reply