bidrectional foregin key one to one
This commit is contained in:
@@ -3,11 +3,9 @@ package org.baeldung.spring.persistence.model;
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.OneToOne;
|
||||
import javax.persistence.PrimaryKeyJoinColumn;
|
||||
|
||||
@Entity
|
||||
public class Child implements Serializable {
|
||||
@@ -16,8 +14,7 @@ public class Child implements Serializable {
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToOne(fetch = FetchType.LAZY)
|
||||
@PrimaryKeyJoinColumn
|
||||
@OneToOne(mappedBy = "child")
|
||||
private Parent parent;
|
||||
|
||||
public Child() {
|
||||
@@ -34,7 +31,6 @@ public class Child implements Serializable {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@OneToOne(mappedBy = "child")
|
||||
public Parent getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ public class Parent implements Serializable {
|
||||
@GeneratedValue
|
||||
private long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "child_fk")
|
||||
private Child child;
|
||||
|
||||
public Parent() {
|
||||
@@ -38,8 +40,6 @@ public class Parent implements Serializable {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
@JoinColumn(name = "child_fk")
|
||||
public Child getChild() {
|
||||
return child;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user