Fix #216 Repository vs DAO

This commit is contained in:
hoswey 2015-11-27 11:22:33 +08:00
parent 092d48d150
commit 9d4fff6029
12 changed files with 343 additions and 72 deletions

12
pom.xml
View File

@ -11,6 +11,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<hibernate.version>5.0.1.Final</hibernate.version>
<spring.version>4.1.7.RELEASE</spring.version>
<spring-data.version>1.9.0.RELEASE</spring-data.version>
<h2.version>1.4.188</h2.version>
<junit.version>4.12</junit.version>
@ -20,6 +21,7 @@
<commons-dbcp.version>1.4</commons-dbcp.version>
<camel.version>2.15.3</camel.version>
<log4j.version>1.2.17</log4j.version>
<guava.version>18.0</guava.version>
</properties>
<modules>
<module>abstract-factory</module>
@ -101,6 +103,11 @@
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data.version}</version>
@ -142,6 +149,11 @@
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.5 KiB

After

Width:  |  Height:  |  Size: 54 KiB

View File

@ -19,10 +19,48 @@
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<dependency id="3">
<class id="3" language="java" name="com.iluwatar.repository.PersonSpecifications.AgeBetweenSpec" project="repository"
file="/repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="412" y="246"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.repository.PersonSpecifications" project="repository"
file="/repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="627" y="68"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="com.iluwatar.repository.PersonSpecifications.NameEqualSpec" project="repository"
file="/repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="834" y="238"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<nesting id="6">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="3"/>
</nesting>
<dependency id="7">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</dependency>
<nesting id="8">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="5"/>
</nesting>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>

View File

@ -25,3 +25,9 @@ querying is utilized.
**Real world examples:**
* [Spring Data](http://projects.spring.io/spring-data/)
**Credits:**
* [Dont use DAO, use Repository](http://thinkinginobjects.com/2012/08/26/dont-use-dao-use-repository/)
* [Advanced Spring Data JPA - Specifications and Querydsl](https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/)

View File

@ -1,35 +1,42 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.8.0-SNAPSHOT</version>
</parent>
<artifactId>repository</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.8.0-SNAPSHOT</version>
</parent>
<artifactId>repository</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -5,7 +5,6 @@ import java.util.List;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* Repository pattern mediates between the domain and data mapping layers using a collection-like
* interface for accessing domain objects. A system with complex domain model often benefits from a
* layer that isolates domain objects from the details of the database access code and in such
@ -16,9 +15,9 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
* <p>
* In this example we utilize Spring Data to automatically generate a repository for us from the
* {@link Person} domain object. Using the {@link PersonRepository} we perform CRUD operations on
* the entity. Underneath we have configured in-memory H2 database for which schema is created and
* dropped on each run.
*
* the entity, moreover, the query by {@link org.springframework.data.jpa.domain.Specification} are
* also performed. Underneath we have configured in-memory H2 database for which schema is created
* and dropped on each run.
*/
public class App {
@ -28,16 +27,21 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
PersonRepository repository = context.getBean(PersonRepository.class);
Person peter = new Person("Peter", "Sagan");
Person nasta = new Person("Nasta", "Kuzminova");
Person peter = new Person("Peter", "Sagan", 17);
Person nasta = new Person("Nasta", "Kuzminova", 25);
Person john = new Person("John", "lawrence", 35);
Person terry = new Person("Terry", "Law", 36);
// Add new Person records
repository.save(peter);
repository.save(nasta);
repository.save(john);
repository.save(terry);
// Count Person records
System.out.println("Count Person records: " + repository.count());
@ -48,9 +52,6 @@ public class App {
System.out.println(person);
}
// Find Person by surname
System.out.println("Find by surname 'Sagan': " + repository.findBySurname("Sagan"));
// Update Person
nasta.setName("Barbora");
nasta.setSurname("Spotakova");
@ -61,9 +62,22 @@ public class App {
// Remove record from Person
repository.delete(2L);
// And finally count records
// count records
System.out.println("Count Person records: " + repository.count());
// find by name
Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
System.out.println("Find by John is " + p);
// find by age
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
System.out.println("Find Person with age between 20,40: ");
for (Person person : persons) {
System.out.println(person);
}
context.close();
}
}

View File

@ -18,11 +18,14 @@ public class Person {
private String name;
private String surname;
private int age;
public Person() {}
public Person(String name, String surname) {
public Person(String name, String surname, int age) {
this.name = name;
this.surname = surname;
this.age = age;
}
public Long getId() {
@ -49,8 +52,59 @@ public class Person {
this.surname = surname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]";
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Person other = (Person) obj;
if (age != other.age)
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (surname == null) {
if (other.surname != null)
return false;
} else if (!surname.equals(other.surname))
return false;
return true;
}
}

View File

@ -1,7 +1,6 @@
package com.iluwatar.repository;
import java.util.List;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
@ -11,7 +10,8 @@ import org.springframework.stereotype.Repository;
*
*/
@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {
public interface PersonRepository
extends CrudRepository<Person, Long>, JpaSpecificationExecutor<Person> {
public List<Person> findBySurname(String surname);
public Person findByName(String name);
}

View File

@ -0,0 +1,50 @@
package com.iluwatar.repository;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
/**
* Helper class, includes vary Specification as the abstraction of sql query criteria
*/
public class PersonSpecifications {
public static class AgeBetweenSpec implements Specification<Person> {
private int from;
private int to;
public AgeBetweenSpec(int from, int to) {
this.from = from;
this.to = to;
}
@Override
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.between(root.get("age"), from, to);
}
}
public static class NameEqualSpec implements Specification<Person> {
public String name;
public NameEqualSpec(String name) {
this.name = name;
}
public Predicate toPredicate(Root<Person> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
return cb.equal(root.get("name"), this.name);
}
}
}

View File

@ -6,10 +6,10 @@
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd">
<jpa:repositories base-package="com.iluwatar" />
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">
<jpa:repositories base-package="com.iluwatar" base-class="org.springframework.data.jpa.repository.support.SimpleJpaRepository" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

View File

@ -1,19 +0,0 @@
package com.iluwatar.repository;
import org.junit.Test;
import com.iluwatar.repository.App;
/**
*
* Application test
*
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,109 @@
package com.iluwatar.repository;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.google.common.collect.Lists;
/**
* Test case to test the functions of {@link PersonRepository}, beside the CRUD functions, the query
* by {@link org.springframework.data.jpa.domain.Specification} are also test.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class RepositoryTest {
@Resource
private PersonRepository repository;
Person peter = new Person("Peter", "Sagan", 17);
Person nasta = new Person("Nasta", "Kuzminova", 25);
Person john = new Person("John", "lawrence", 35);
Person terry = new Person("Terry", "Law", 36);
List<Person> persons = Arrays.asList(peter, nasta, john, terry);
/**
* Prepare data for test
*/
@Before
public void setup() {
repository.save(persons);
}
@Test
public void testFindAll() {
List<Person> actuals = Lists.newArrayList(repository.findAll());
assertTrue(actuals.containsAll(persons) && persons.containsAll(actuals));
}
@Test
public void testSave() {
Person terry = repository.findByName("Terry");
terry.setSurname("Lee");
terry.setAge(47);
repository.save(terry);
terry = repository.findByName("Terry");
assertEquals(terry.getSurname(), "Lee");
assertEquals(47, terry.getAge());
}
@Test
public void testDelete() {
Person terry = repository.findByName("Terry");
repository.delete(terry);
assertEquals(3, repository.count());
assertNull(repository.findByName("Terry"));
}
@Test
public void testCount() {
assertEquals(4, repository.count());
}
@Test
public void testFindAllByAgeBetweenSpec() {
List<Person> persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
assertEquals(3, persons.size());
assertTrue(persons.stream().allMatch((item) -> {
return item.getAge() > 20 && item.getAge() < 40;
}));
}
@Test
public void testFindOneByNameEqualSpec() {
Person actual = repository.findOne(new PersonSpecifications.NameEqualSpec("Terry"));
assertEquals(terry, actual);
}
@After
public void cleanup() {
repository.deleteAll();
}
}