persistence work

This commit is contained in:
eugenp 2013-05-18 20:14:59 +03:00
parent 26e6d6eb86
commit 34c89649fe
2 changed files with 16 additions and 3 deletions

View File

@ -16,7 +16,7 @@ public class Parent implements Serializable {
@GeneratedValue
private long id;
@OneToOne(cascade = CascadeType.ALL)
@OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH })
@JoinColumn(name = "child_fk")
private Child child;

View File

@ -7,6 +7,7 @@ import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -46,8 +47,8 @@ public class ParentServicePersistenceIntegrationTest {
System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild());
}
@Test
public final void whenChildIsDeleted_thenDataException() {
@Test(expected = DataIntegrityViolationException.class)
public final void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() {
final Child childEntity = new Child();
childService.create(childEntity);
@ -57,4 +58,16 @@ public class ParentServicePersistenceIntegrationTest {
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);
}
}