persistence work

This commit is contained in:
eugenp 2013-05-18 13:07:16 +03:00
parent 09eaa5b8e1
commit 2b7304c32d
3 changed files with 172 additions and 145 deletions

View File

@ -1,5 +1,4 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>spring-hibernate4</artifactId>
@ -47,6 +46,14 @@
<scope>runtime</scope>
</dependency>
<!-- validation -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>
<!-- utils -->
<dependency>

View File

@ -5,6 +5,7 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
@Entity
public class Foo {
@ -14,6 +15,7 @@ public class Foo {
private long id;
@Column(nullable = false)
@NotNull
private String name;
public Foo() {

View File

@ -7,6 +7,8 @@ import org.baeldung.spring.persistence.model.Foo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
@ -30,10 +32,26 @@ public class FooServicePersistenceIntegrationTest {
service.create(new Foo(randomAlphabetic(6)));
}
// @Test(expected = DataIntegrityViolationException.class)
@Test
@Test(expected = DataIntegrityViolationException.class)
public final void whenInvalidEntityIsCreated_thenDataException() {
service.create(new Foo());
}
@Test(expected = DataIntegrityViolationException.class)
public final void whenEntityWithLongNameIsCreated_thenDataException() {
service.create(new Foo(randomAlphabetic(2048)));
}
@Test(expected = InvalidDataAccessApiUsageException.class)
public final void whenSameEntityIsCreatedTwice_thenDataException() {
final Foo entity = new Foo(randomAlphabetic(8));
service.create(entity);
service.create(entity);
}
@Test
public final void temp_whenInvalidEntityIsCreated_thenDataException() {
service.create(new Foo(randomAlphabetic(2048)));
}
}