18 June 2014

OneToOne Relationship

Defines a single-valued association to another entity. Bidirectional relationship must refer to its owning side by use of the mappedBy attribute.



BankAccount.java
package org.metadata.jee.jpa.entities;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class BankAccount implements Serializable {
      @Id
      private String id;
      private String account;

      public String getId() {
            return id;
      }

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

      public String getAccount() {
            return account;
      }

      public void setAccount(String account) {
            this.account = account;
      }
}

Employee.java
package org.metadata.jee.jpa.entities;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

@Entity
@Table(name = "EMP_TBL")
@TableGenerator(name = "EMP_GENTOR", table = "ID_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL", pkColumnValue = "EMP_GEN", allocationSize = 10)
public class Employee implements Serializable {
      @Id
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "EMP_GENTOR")
      private String id;
      private String name;
      @OneToOne
      @JoinColumn(name = "BANKACCOUNT_ID", referencedColumnName = "ID")
      private BankAccount bankAccount;

      public String getId() {
            return id;
      }

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

      public String getName() {
            return name;
      }

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

      public BankAccount getBankAccount() {
            return bankAccount;
      }

      public void setBankAccount(BankAccount bankAccount) {
            this.bankAccount = bankAccount;
      }
}


EmployeeTest.java
package org.metadata.jee.jpa.entities.test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import org.metadata.jee.jpa.entities.BankAccount;
import org.metadata.jee.jpa.entities.Employee;

public class EmployeeTest {
      private EntityManagerFactory emf;

      public EmployeeTest() {
            emf = Persistence.createEntityManagerFactory("METADATA_JPA");
      }

      public String insert() {
            EntityManager em = emf.createEntityManager();
            // Begin Transaction
            em.getTransaction().begin();
            BankAccount bankAccount = em.find(BankAccount.class, "1");
            Employee employee = new Employee();
            employee.setName("Mr Jone");
            employee.setBankAccount(bankAccount);

            // Insert employee to database
            em.persist(employee);

            // Commit Transaction
            em.getTransaction().commit();
            return "OK";
      }

      public static void main(String[] args) {
            EmployeeTest employeeTest = new EmployeeTest();
            String status = employeeTest.insert();
            if (status.equals("OK")) {
                  System.out.println("Insert Employee test is success.");
            }
      }
}

persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="METADATA_JPA" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
      <class>org.metadata.jee.jpa.entities.Employee</class>
      <class>org.metadata.jee.jpa.entities.BankAccount</class>   
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/metadatajpa"/>
      <property name="javax.persistence.jdbc.user" value="metadatajpa"/>
        <property name="javax.persistence.jdbc.password" value="metadatajpa"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>     
    </properties>
  </persistence-unit>
</persistence>

create.sql
CREATE TABLE EMP_TBL (
      ID VARCHAR(255) NOT NULL,
      NAME VARCHAR(255),
      BANKACCOUNT_ID VARCHAR(255), PRIMARY KEY (ID)
);
CREATE TABLE BANKACCOUNT (
      ID VARCHAR(255) NOT NULL,
      ACCOUNT VARCHAR(255), PRIMARY KEY (ID)
);
ALTER TABLE EMP_TBL ADD CONSTRAINT FK_EMP_TBL_BANKACCOUNT_ID FOREIGN KEY (BANKACCOUNT_ID) REFERENCES BANKACCOUNT (ID);
CREATE TABLE ID_GEN (
      GEN_NAME VARCHAR(50) NOT NULL,
      GEN_VAL DECIMAL(38), PRIMARY KEY (GEN_NAME)
);
INSERT INTO ID_GEN(GEN_NAME, GEN_VAL) values ('EMP_GEN', 0);
INSERT INTO BANKACCOUNT(ID, ACCOUNT) values ('1', 'BAK00001');

No comments:

Post a Comment

Like us on Facebook