Home > Java > Hibernate One To One Annotation Mapping [ Part 3 ]

Hibernate One To One Annotation Mapping [ Part 3 ]

November 28, 2013 Leave a comment Go to comments

Berikut ini contoh asosiasi One To One mapping dengan strategy shared primary key

1. Table

Table manager

Table Manager

Table Manager

Table company_department

Table Company_Department

Table Company_Department

 

2. Entity Class

Department class

package pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="company_department")
public class Department {
@Id
@GeneratedValue(generator="gen")
@GenericGenerator(name="gen", strategy="foreign",parameters=@Parameter(name="property", value="manager"))
@Column(name="id")
private int idDepartment;
@Column(name="department_name")
private String departmentName;
@Column(name="location")
private String location;
@OneToOne
@PrimaryKeyJoinColumn
private Manager manager;

public int getIdDepartment() {
return idDepartment;
}

public void setIdDepartment(int idDepartment) {
this.idDepartment = idDepartment;
}

public String getDepartmentName() {
return departmentName;
}

public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public Manager getManager() {
return manager;
}

public void setManager(Manager manager) {
this.manager = manager;
}

}

Manager class

package pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name="manager")
public class Manager {

@Id
@Column(name="id_manager")
@GeneratedValue
private int idManager;
@Column(name="manager_name")
private String managerName;
@Column(name="phone_number")
private String phoneNumber;

@OneToOne(mappedBy="manager")
private Department department;

public int getIdManager() {
return idManager;
}

public void setIdManager(int idManager) {
this.idManager = idManager;
}

public String getManagerName() {
return managerName;
}

public void setManagerName(String managerName) {
this.managerName = managerName;
}

public String getPhoneNumber() {
return phoneNumber;
}

public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public Department getDepartment() {
return department;
}

public void setDepartment(Department department) {
this.department = department;
}

}

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 onetoonec;
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 onetoonec;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Department;
import pojo.Manager;

/**
*
* @author Kukuh Utama
*/
public class OneToOneC {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Department department = new Department();
department.setDepartmentName("IT Touchpoint");
department.setLocation("Grha XL Lt.8");

Manager manager = new Manager();
manager.setManagerName("Aerlangga H");
manager.setPhoneNumber("021 322384");

manager.setDepartment(department);
department.setManager(manager);

session.save(manager);
session.save(department);
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: