Home > Java > Hibernate Many To Many Annotation Mapping

Hibernate Many To Many Annotation Mapping

1. Table

Table certificate

Table Certificate

Table Certificate

Table employee

Table Employee

Table Employee

Table employee_certificate

Table Employee_Certificate

Table Employee_Certificate

2. Entity Class

Certificate class

package pojo;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="certificate")
public class Certificate {

@Id
@GeneratedValue
@Column(name="certificate_id")
private int certificateId;

@Column(name="certificate_name")
private String certificateName;

@ManyToMany(mappedBy="certificate")
private Set<Employee> employee = new HashSet<>();

public int getCertificateId() {
return certificateId;
}

public void setCertificateId(int certificateId) {
this.certificateId = certificateId;
}

public String getCertificateName() {
return certificateName;
}

public void setCertificateName(String certificateName) {
this.certificateName = certificateName;
}

public Set<Employee> getEmployee() {
return employee;
}

public void setEmployee(Set<Employee> employee) {
this.employee = employee;
}

}

Employee class

package pojo;

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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class Employee {

@Id
@GeneratedValue
@Column(name="employee_id")
private int employeeId;
@Column(name="employee_name")
private String employeeName;
@Column(name="employee_department")
private String employeeDepartment;

@ManyToMany(cascade= CascadeType.ALL)
@JoinTable(name="employee_certificate", joinColumns={@JoinColumn(name="employee_id", referencedColumnName="employee_id")}, inverseJoinColumns={@JoinColumn(name="certificate_id", referencedColumnName="certificate_id")})
private Set<Certificate> certificate = new HashSet<>(0);

public int getEmployeeId() {
return employeeId;
}

public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}

public String getEmployeeName() {
return employeeName;
}

public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}

public String getEmployeeDepartment() {
return employeeDepartment;
}

public void setEmployeeDepartment(String employeeDepartment) {
this.employeeDepartment = employeeDepartment;
}

public Set<Certificate> getCertificate() {
return certificate;
}

public void setCertificate(Set<Certificate> certificate) {
this.certificate = certificate;
}

}

3. Hibernate Configuration File

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jointrain?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql"> true </property>
<mapping/>
<mapping/>
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package manytomanya;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {

return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}

static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

5. Main Class

package manytomanya;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.*;

public class ManyToManyA {

public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Certificate certificate1 = new Certificate();
certificate1.setCertificateName("Java");

Certificate certificate2 = new Certificate();
certificate2.setCertificateName("CCNA");

Certificate certificate3 = new Certificate();
certificate3.setCertificateName("CISCO");

Certificate certificate4 = new Certificate();
certificate4.setCertificateName("PHP");

Employee employee1 = new Employee();
employee1.setEmployeeName("Herling Setiabudi");
employee1.setEmployeeDepartment("IT Touchpoint");

Employee employee2 = new Employee();
employee2.setEmployeeName("Lax Utama");
employee2.setEmployeeDepartment("IT Network");

Set<Certificate> certificateSet1 = new HashSet<>();
certificateSet1.add(certificate4);
certificateSet1.add(certificate1);

Set<Certificate> certificateSet2 = new HashSet<>();
certificateSet2.add(certificate2);
certificateSet2.add(certificate3);

employee1.setCertificate(certificateSet1 );
employee2.setCertificate(certificateSet2);

session.save(employee1);
session.save(employee2);

session.getTransaction().commit();
session.close();
}
}

6. Project Structure

Project Structure

Project Structure

Semoga bermanfaat.

Referensi : dari berbagai sumber.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: