bidrectional foregin key one to one
This commit is contained in:
parent
fbb5402bf2
commit
cb1c8cb3e2
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -28,7 +28,7 @@ public class ParentServicePersistenceIntegrationTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public final void whenEntityIsCreated_thenNoExceptions() {
|
||||
public final void whenOneToOneEntitiesAreCreated_thenNoExceptions() {
|
||||
final Child childEntity = new Child();
|
||||
childService.create(childEntity);
|
||||
|
||||
|
@ -42,4 +42,15 @@ public class ParentServicePersistenceIntegrationTest {
|
|||
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
|
||||
}
|
||||
|
||||
@Test
|
||||
public final void whenChildIsDeleted_thenDataException() {
|
||||
final Child childEntity = new Child();
|
||||
childService.create(childEntity);
|
||||
|
||||
final Parent parentEntity = new Parent(childEntity);
|
||||
service.create(parentEntity);
|
||||
|
||||
childService.delete(childEntity);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue