Hibernate Annotations - OnetoOne Mapping

Hibernate Annotations JPA 1

 

One to One Mapping

 

Class Employee.java

package com.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "employeeaddress")
public class EmployeeAddress implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    // Fields

    private Integer employeeid;

    private String address;

    private Employee employee;

    // Constructors

    /** default constructor */
    public EmployeeAddress() {
    }

    /** minimal constructor */
    public EmployeeAddress(Integer employeeid) {
        this.employeeid = employeeid;
    }

    /** full constructor */
    public EmployeeAddress(Integer employeeid, String address) {
        this.employeeid = employeeid;
        this.address = address;
    }

    // Property accessors
    @Id
    @Column(name = "EMPLOYEEID", unique = true, nullable = false, insertable = true, updatable = true)
    public Integer getEmployeeid() {
        return this.employeeid;
    }

   
    public void setEmployeeid(Integer employeeid) {
        this.employeeid = employeeid;
    }

    @Column(name = "ADDRESS", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @OneToOne
    @PrimaryKeyJoinColumn
    public Employee getEmployee() {
        return employee;
    }

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

Class EmployeeAddress.java


package com.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "employeeaddress")
public class EmployeeAddress implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    // Fields

    private Integer employeeid;

    private String address;

    private Employee employee;

    // Constructors

    /** default constructor */
    public EmployeeAddress() {
    }

    /** minimal constructor */
    public EmployeeAddress(Integer employeeid) {
        this.employeeid = employeeid;
    }

    /** full constructor */
    public EmployeeAddress(Integer employeeid, String address) {
        this.employeeid = employeeid;
        this.address = address;
    }

    // Property accessors
    @Id
    @Column(name = "EMPLOYEEID", unique = true, nullable = false, insertable = true, updatable = true)
    public Integer getEmployeeid() {
        return this.employeeid;
    }

   
    public void setEmployeeid(Integer employeeid) {
        this.employeeid = employeeid;
    }

    @Column(name = "ADDRESS", unique = false, nullable = true, insertable = true, updatable = true, length = 45)
    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @OneToOne
    @PrimaryKeyJoinColumn
    public Employee getEmployee() {
        return employee;
    }

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

}

Test method to save the Employee object

public void test() {
      
        Employee e = new Employee();
        e.setEmployeeid(new Random().nextInt(10000));
        e.setName("MyName");
        EmployeeAddress ed = new EmployeeAddress(); 
        ed.setEmployeeid(e.getEmployeeid());
       
        ed.setAddress("MyCity");
        ed.setEmployee(e);
        e.setEmployeeaddress(ed);

        // Create session with AnnotationConfiguration
        Session session = null;
        try {
            SessionFactory sf =  new AnnotationConfiguration().configure().buildSessionFactory();
            session = sf.openSession();
            session.beginTransaction();
            session.save(e);
            session.getTransaction().commit();
        } catch (HibernateException ex) {
            ex.printStackTrace();
        }finally{
            if(session != null) session.close();
        }
      
    }

Comments

Popular posts from this blog

Log4j multiple WAR files in single EAR configuration

Java NIO2 - Watching a directory for changes. FileWatcherService.

Ubuntu Add programs to launcher (Desktop)