persistence work
This commit is contained in:
parent
26e6d6eb86
commit
34c89649fe
@ -16,7 +16,7 @@ public class Parent implements Serializable {
|
|||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@OneToOne(cascade = CascadeType.ALL)
|
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
|
||||||
@JoinColumn(name = "child_fk")
|
@JoinColumn(name = "child_fk")
|
||||||
private Child child;
|
private Child child;
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ import org.hibernate.SessionFactory;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.dao.DataIntegrityViolationException;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
@ -46,8 +47,8 @@ public class ParentServicePersistenceIntegrationTest {
|
|||||||
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
|
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test(expected = DataIntegrityViolationException.class)
|
||||||
public final void whenChildIsDeleted_thenDataException() {
|
public final void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() {
|
||||||
final Child childEntity = new Child();
|
final Child childEntity = new Child();
|
||||||
childService.create(childEntity);
|
childService.create(childEntity);
|
||||||
|
|
||||||
@ -57,4 +58,16 @@ public class ParentServicePersistenceIntegrationTest {
|
|||||||
childService.delete(childEntity);
|
childService.delete(childEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void whenChildIsDeletedAfterTheParent_thenNoExceptions() {
|
||||||
|
final Child childEntity = new Child();
|
||||||
|
childService.create(childEntity);
|
||||||
|
|
||||||
|
final Parent parentEntity = new Parent(childEntity);
|
||||||
|
service.create(parentEntity);
|
||||||
|
|
||||||
|
service.delete(parentEntity);
|
||||||
|
childService.delete(childEntity);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user