Close

JPA - @OneToOne Examples

JPA JAVA EE 

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
@OneToOne
private EntityB entityB;
.............
}

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String stringB;
.............
}
Original Post




@Entity
public class EntityC {
@Id
@GeneratedValue
private int myIdC;
@OneToOne(cascade = CascadeType.PERSIST)
private EntityD entityD;
.............
}

@Entity
public class EntityD {
@Id
@GeneratedValue
private int myIdD;
private String stringD;
.............
}
Original Post




Making @OneToOne relationship mandatory by setting optional=false:

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
@OneToOne(optional = false)
private EntityB entityB;
.............
}

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String stringB;
.............
}
Original Post




@OneToOne bidirectional:

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
@OneToOne
private EntityB refEntityB;
.............
}

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
@OneToOne(mappedBy = "refEntityB")
private EntityA refEntityA;
private String str;
.............
}
Original Post

@OneToOne unidirectional with @JoinTable:

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
private String stringA;
@OneToOne
@JoinTable(name = "MY_JOIN_TABLE",
joinColumns = {
@JoinColumn(name = "ENTITYA_FK", referencedColumnName = "myIdA")
},
inverseJoinColumns = {
@JoinColumn(name = "ENTITYB_FK", referencedColumnName = "myIdB", unique = true)
}
)
private EntityB entityB;
.............
}

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String stringB;
.............
}
Original Post




@OneToOne bidirectional with @JoinTable:

@Entity
public class EntityA {
@Id
@GeneratedValue
private int myIdA;
private String stringA;
@OneToOne
@JoinTable(name = "MY_JOIN_TABLE",
joinColumns = {
@JoinColumn(name = "ENTITYA_FK", referencedColumnName = "myIdA")
},
inverseJoinColumns = {
@JoinColumn(name = "ENTITYB_FK", referencedColumnName = "myIdB", unique = true)
}
)
private EntityB entityB;
.............
}

@Entity
public class EntityB {
@Id
@GeneratedValue
private int myIdB;
private String stringB;

@OneToOne(mappedBy = "entityB")
private EntityA entityA;
.............
}
Original Post




import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.OneToOne;
import java.util.Date;

@Entity
@IdClass(CompositeTaskId.class)
public class Task {
@Id
private long taskId;
@Id
@OneToOne
private Employee employee;
private String taskName;
private Date date;

public Task() {
}

public Task(Employee employee, long taskId) {
this.employee = employee;
this.taskId = taskId;
}

public String getTaskName() {
return taskName;
}

public void setTaskName(String taskName) {
this.taskName = taskName;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}

@Override
public String toString() {
return "Task{" +
"taskId=" + taskId +
", employee=" + employee +
", taskName='" + taskName + '\'' +
", date=" + date +
'}';
}
}
import java.io.Serializable;
import java.util.Objects;

public class CompositeTaskId implements Serializable {
//the names of the both fields should be same as the @Id fields in source class
//the type of 'taskId' field is same as of the basic @Id field defined in the Task entity
private long taskId;
//the type of 'employee' field is same as of the @Id field defined in Employee entity.
private long employee;

public CompositeTaskId() {
}

public CompositeTaskId(long employeeId, long taskId) {
this.employee = employeeId;
this.taskId = taskId;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CompositeTaskId taskId1 = (CompositeTaskId) o;
if (employee != taskId1.employee) return false;
return taskId == taskId1.taskId;
}

@Override
public int hashCode() {
return Objects.hash(employee, taskId);
}
}
Original Post




See Also