good one to one mapping, failing tests for constraint violation

This commit is contained in:
eugenp 2013-05-18 18:11:09 +03:00
parent cb1c8cb3e2
commit 26e6d6eb86
5 changed files with 9 additions and 3 deletions

View File

@ -35,7 +35,8 @@ public abstract class AbstractHibernateDao<T extends Serializable> implements IO
@Override
public final void create(final T entity) {
Preconditions.checkNotNull(entity);
getCurrentSession().persist(entity);
// getCurrentSession().persist(entity);
getCurrentSession().saveOrUpdate(entity);
}
@Override

View File

@ -44,7 +44,7 @@ public class Child implements Serializable {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Child [id=").append(id).append(", parent=").append(parent).append("]");
builder.append("Child [id=").append(id).append("]");
return builder.toString();
}

View File

@ -53,7 +53,7 @@ public class Parent implements Serializable {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Parent [id=").append(id).append(", child=").append(child).append("]");
builder.append("Parent [id=").append(id).append("]");
return builder.toString();
}

View File

@ -44,6 +44,7 @@ public class FooServicePersistenceIntegrationTest {
}
@Test(expected = InvalidDataAccessApiUsageException.class)
@Ignore("Right now, persist has saveOrUpdate semantics, so this will no longer fail")
public final void whenSameEntityIsCreatedTwice_thenDataException() {
final Foo entity = new Foo(randomAlphabetic(8));
service.create(entity);

View File

@ -3,6 +3,7 @@ package org.baeldung.spring.persistence.service;
import org.baeldung.spring.persistence.config.PersistenceConfig;
import org.baeldung.spring.persistence.model.Child;
import org.baeldung.spring.persistence.model.Parent;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -20,6 +21,9 @@ public class ParentServicePersistenceIntegrationTest {
@Autowired
private IChildService childService;
@Autowired
private SessionFactory sessionFactory;
// tests
@Test