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

Hibernate One To One Annotation Mapping [ Part 1 ]

November 26, 2013 Leave a comment Go to comments

Berikut ini contoh asosiasi One To One mapping dengan strategy foreign key association

1. Table

Table baccount

Account Table

Account Table

Table bcustomer

Customer Table

Customer Table

2. Entity Class

BAccount 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="baccount")
public class BAccount {

private int id;
private String accountNumber;
private BCustomer bCustomer;

@Id
@Column(name="Id")
@GeneratedValue
public int getId() {
return id;
}

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

@Column(name="account_number")
public String getAccountNumber() {
return accountNumber;
}

public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}

@OneToOne(mappedBy="bAccount")
public BCustomer getbCustomer() {
return bCustomer;
}

public void setbCustomer(BCustomer bCustomer) {
this.bCustomer = bCustomer;
}
}

BCustomer class.


import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name="bcustomer")
public class BCustomer implements Serializable {

@Id
@Column(name="Id")
@GeneratedValue
private int Id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;

@OneToOne
@JoinColumn(name="id_account")
private BAccount bAccount;

public int getId() {
return Id;
}

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

public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public BAccount getbAccount() {
return bAccount;
}

public void setbAccount(BAccount bAccount) {
this.bAccount = bAccount;
}
}

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 class="pojo.BAccount"/>
<mapping class="pojo.BCustomer"/>
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package onetoonea;

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


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.BAccount;
import pojo.BCustomer;


public class OneToOneA {
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();
BAccount bAccount = new BAccount();
bAccount.setAccountNumber("12345A");


BCustomer bCustomer = new BCustomer();
bCustomer.setFirstName("Kukuh");
bCustomer.setLastName("Utama");


bAccount.setbCustomer(bCustomer);
bCustomer.setbAccount(bAccount);


session.save(bAccount);
session.save(bCustomer);
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: