Question
How do you implement one to one relationship in Hibernate with java annotation

Answers

Taking the same Employee and Employee Details example of the above question.
Employee.javapackage com.test.onetoone.mappingimport javax.persistence.CascadeType;import javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import static javax.persistence.GenerationType.IDENTITY;import javax.persistence.Id;import javax.persistence.OneToOne;import javax.persistence.Table; @Entity@Table(name = "emp") public class Employee implements java.io.Serializable{   private Integer empId;   private EmployeeDetail empDetail;    public Employee(){}   @Id   @GeneratedValue(strategy = IDENTITY)   @Column(name = "EMP_ID", unique = true, nullable = false)   public Integer getEmpId()   {       return this.empId;   }   public void setEmpId(Integer empId)   {       this.empId = empId;   }   @OneToOne(fetch = FetchType.LAZY, mappedBy = "emp", cascade = CascadeType.ALL)   public EmployeeDetail getEmpDetail()    {       return this.empDetail;   }    public void setEmpDetail(EmployeeDetail empDetail)    {       this.empDetail = empDetail;    } }
File: EmployeeDetail.java
package com.test.onetoone.mappingimport javax.persistence.Column;import javax.persistence.Entity;import javax.persistence.FetchType;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.OneToOne;import javax.persistence.PrimaryKeyJoinColumn;import javax.persistence.Table;import javax.persistence.Temporal;import javax.persistence.TemporalType;import org.hibernate.annotations.GenericGenerator;import org.hibernate.annotations.Parameter; @Entity@Table(name = "emp_detail")public class EmployeeDetail implements java.io.Serializable {    private Integer empId;   private Employee emp;   private String name;   private String address;    public EmployeeDetail() {}    public EmployeeDetail(Employee emp, String name, String address)    {       this.emp = emp;       this.name = name;       this.address = address;   }    @GenericGenerator(name = "generator", strategy = "foreign",    parameters = @Parameter(name = "property", value = "emp"))   @Id   @GeneratedValue(generator = "generator")   @Column(name = "EMP_ID", unique = true, nullable = false)   public Integer getEmpId()    {       return this.empId;   }    public void setEmpId(Integer empId)    {       this.empId = empId;   }    @OneToOne(fetch = FetchType.LAZY)   @PrimaryKeyJoinColumn   public Employee getEmp()    {       return this.emp;    }   public void setEmp(Employee emp)    {       this.emp = emp;   }    @Column(name = "ADDRESS", nullable = false, length = 400)   public String getAddress()    {       return this.address;   }    public void setAddress(String address)    {       this.address = address;   }    @Column(name = "EMP_NAME", nullable = false)   public String getName()    {       return this.name;   }    public void setName(String name)    {       this.name = name;   } }3. Hibernate Configuration File- Puts annotated classes Employee.java and EmployeeDetail.java in your Hibernate configuration file, and also MySQL connection details.File : hibernate.cfg.xmlcom.mysql.jdbc.Driverjdbc:mysql://localhost:3306/mytestdbrootpasswordorg.hibernate.dialect.MySQLDialecttrue   Your Comment




More Software,Java Questions..
What is the inputsplit in map reduce software?

Can variables be used in Java without initialization?

What is the difference between java bean and enterprise java bean?

What is software configuration management?

How does the java database connectivity (jdbc) work?

What are the advantages of threads in java?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

What is object cloning in Java?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?



Search
Can you Answer!!
  • Q What is difference between drill & scope of analysis?
  • Q Whose paintings included Woman with a Hat, The Open Window, and Bathers with a Turtle?
  • Q If a salesman's average is a new order every other week, he will break the office record of the year. However, after 28 weeks, he is six orders behind schedule. In what proportion of the remaining weeks does he have to obtain a new order to break the record?
  • Q How can art be musical?
  • Q Discuss in detail the measures to be taken for disaster management.
  • Q John Hetherington in London introduced what in 1797
  • Q What is Model, View and Controller in an MVC application?
  • Q An advantage of the use of hot sites as a backup alternative is that: A. the costs associated with hot sites are low. B. hot sites can be used for an extended amount of time. C. hot sites can be made ready for operation within a short period of time. D. they do not require that equipment and systems software be compatible with the primary site.
  • Q Who are the best art photographers?
  • Q What is heteroplastic transplantation?
  • Q Differentiate between power and authority.