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

Hibernate One To One Annotation Mapping [ Part 2 ]

November 27, 2013 Leave a comment Go to comments

Berikut ini contoh asosiasi One To One mapping dengan strategy join table.

1. Table

Table husband

Table Husband

Table Husband

Table wife

Table Wife

Table Wife

Table husband_wife

Table Husband_Wife

Table Husband_Wife

2. Entity Class

Husband class

package pojo;

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.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="husband")
public class Husband {

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

@Column(name="name_husband")
private String husbandName;

@OneToOne
@JoinTable(name="husband_wife", joinColumns=@JoinColumn(name="husband_id"), inverseJoinColumns=@JoinColumn(name="wife_id"))
private Wife wife;

public int getId() {
return Id;
}

public void setId(int Id) {
this.Id = Id;
}

public String getHusbandName() {
return husbandName;
}

public void setHusbandName(String husbandName) {
this.husbandName = husbandName;
}

public Wife getWife() {
return wife;
}

public void setWife(Wife wife) {
this.wife = wife;
}
}

Wife 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="wife")
public class Wife {
@Id
@GeneratedValue
@Column(name="id")
private int Id;

@Column(name="name_wife")
private String wifeName;

@OneToOne(mappedBy="wife")
private Husband husband;

public int getId() {
return Id;
}

public void setId(int Id) {
this.Id = Id;
}

public String getWifeName() {
return wifeName;
}

public void setWifeName(String wifeName) {
this.wifeName = wifeName;
}

public Husband getHusband() {
return husband;
}

public void setHusband(Husband husband) {
this.husband = husband;
}
}

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>
<mapping></mapping>
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package onetooneb;

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

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

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().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);
}
}

static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

5. Main Class

package onetooneb;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Husband;
import pojo.Wife;

/**
*
* @author Kukuh Utama
* Using foreign join table
*/
public class OneToOneB {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
Wife wife = new Wife();
wife.setWifeName("Finita Ari");

Husband husband = new Husband();
husband.setHusbandName("Herling S");

wife.setHusband(husband);
husband.setWife(wife);
session.save(husband);
session.save(wife);

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

6. Project Structure

Project Structure

Project Structure

7. Output Console

Output Console

Output Console


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: