This example illustrates a one-to-one mapping in Hibernate using JPA annotations. The illustration is about a case where a driver has one driving license. Here the license details will be saved to a database table by the name 'license' and that of driver to 'driver'. I'm only discussing the OneToOne mapping here. [For other basic information, refer to Hinernate Annotations Example].
The mappings of the classes should be in the hibernate.cfg.xml file
<mapping class="com.shyarmal.hibernate.Driver"/>
<mapping class="com.shyarmal.hibernate.License"/>
<mapping class="com.shyarmal.hibernate.License"/>
My License class.
===============================================================package com.shyarmal.hibernate;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "license")
public class License {
private Long id;
private Date issueDate;
private String country;
private String category;
private License() {
}
public License(Date issueDate, String country, String category) {
this.issueDate = issueDate;
this.country = country;
this.category = category;
}
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name = "issue_date", columnDefinition = "timestamp")
public Date getIssueDate() {
return issueDate;
}
public void setIssueDate(Date issueDate) {
this.issueDate = issueDate;
}
@Column(name = "country")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Column(name = "category")
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}
===============================================================The Driver class should have an instance of the License class, if to form a one-to-one.
My Driver class
===============================================================
package com.shyarmal.hibernate;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(name = "driver")
public class Driver {
private Long id;
private String name;
private int age;
private License license;
private Driver() {
}
public Driver(String name, int age) {
this.name = name;
this.age = age;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@OneToOne(cascade = CascadeType.ALL)
public License getLicense() {
return license;
}
public void setLicense(License license) {
this.license = license;
}
}
===============================================================
@OneToOne:- This annotation is to make the link between the corresponding field of the annotated class to that intended to be related in one-to-one mapping. The CascadeType is set to ALL which will reflect any change in the 'driver' table to the 'license' table. That means, for an instance if a driver is deleted his license will also be deleted correspondingly.
thanks,
Shyarmal.