persistence work

This commit is contained in:
Eugen 2013-05-17 12:37:04 +03:00
parent 852c5b96de
commit c5ba785246
3 changed files with 61 additions and 1 deletions

View File

@ -56,6 +56,14 @@
</dependency> </dependency>
<!-- test scoped --> <!-- test scoped -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>

View File

@ -1,5 +1,6 @@
package org.baeldung.spring.persistence.model; package org.baeldung.spring.persistence.model;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
@ -12,10 +13,19 @@ public class Foo {
@GeneratedValue(strategy = GenerationType.AUTO) @GeneratedValue(strategy = GenerationType.AUTO)
private long id; private long id;
@Column(nullable = false)
private String name;
public Foo() { public Foo() {
super(); super();
} }
public Foo(final String name) {
super();
this.name = name;
}
// API // API
public long getId() { public long getId() {
@ -26,6 +36,46 @@ public class Foo {
this.id = id; this.id = id;
} }
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
// //
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Foo other = (Foo) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
builder.append("Foo [name=").append(name).append("]");
return builder.toString();
}
} }

View File

@ -1,5 +1,7 @@
package org.baeldung.spring.persistence.service; package org.baeldung.spring.persistence.service;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.baeldung.spring.persistence.config.PersistenceConfig; import org.baeldung.spring.persistence.config.PersistenceConfig;
import org.baeldung.spring.persistence.model.Foo; import org.baeldung.spring.persistence.model.Foo;
import org.junit.Test; import org.junit.Test;
@ -25,7 +27,7 @@ public class FooServicePersistenceIntegrationTest {
@Test @Test
public final void whenEntityisCreated_thenNoExceptions() { public final void whenEntityisCreated_thenNoExceptions() {
service.create(new Foo()); service.create(new Foo(randomAlphabetic(6)));
} }
} }