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

  1. The bizarre media occasion was hoped to make some diplomatic floor by minimal of|no less than} opening the door, but since not much progress has been made and North Korea stays a heavily sanctioned country by most of the world. Several key issues have arisen as increasingly jurisdictions are permitting and controlling on-line playing worldwide. These nations take steps to keep on-line playing responsible, corresponding to a dedicated finances for dependancy facilities and limits on betting quantities . Illegal operators, nevertheless, encourage 바카라사이트 customers to wager massive quantities regularly and keep away from paying taxes.

    ReplyDelete

Post a Comment

Popular posts from this blog

SVN File Creation Date

Ubuntu / kubuntu - Laptop key board cursor jumping issue

Log4j multiple WAR files in single EAR configuration