Skip to content

Commit 5bec636

Browse files
committed
Merge pull request iluwatar#305 from hoswey/master
Fix iluwatar#216 Repository vs DAO
2 parents 092d48d + 9d4fff6 commit 5bec636

File tree

12 files changed

+343
-72
lines changed

12 files changed

+343
-72
lines changed

pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<hibernate.version>5.0.1.Final</hibernate.version>
14+
<spring.version>4.1.7.RELEASE</spring.version>
1415
<spring-data.version>1.9.0.RELEASE</spring-data.version>
1516
<h2.version>1.4.188</h2.version>
1617
<junit.version>4.12</junit.version>
@@ -20,6 +21,7 @@
2021
<commons-dbcp.version>1.4</commons-dbcp.version>
2122
<camel.version>2.15.3</camel.version>
2223
<log4j.version>1.2.17</log4j.version>
24+
<guava.version>18.0</guava.version>
2325
</properties>
2426
<modules>
2527
<module>abstract-factory</module>
@@ -101,6 +103,11 @@
101103
<version>${hibernate.version}</version>
102104
</dependency>
103105
<dependency>
106+
<groupId>org.springframework</groupId>
107+
<artifactId>spring-test</artifactId>
108+
<version>${spring.version}</version>
109+
</dependency>
110+
<dependency>
104111
<groupId>org.springframework.data</groupId>
105112
<artifactId>spring-data-jpa</artifactId>
106113
<version>${spring-data.version}</version>
@@ -142,6 +149,11 @@
142149
<artifactId>log4j</artifactId>
143150
<version>${log4j.version}</version>
144151
</dependency>
152+
<dependency>
153+
<groupId>com.google.guava</groupId>
154+
<artifactId>guava</artifactId>
155+
<version>${guava.version}</version>
156+
</dependency>
145157
</dependencies>
146158
</dependencyManagement>
147159

repository/etc/repository.png

44.5 KB
Loading

repository/etc/repository.ucls

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,48 @@
1919
<operations public="true" package="true" protected="true" private="true" static="true"/>
2020
</display>
2121
</interface>
22-
<dependency id="3">
22+
<class id="3" language="java" name="com.iluwatar.repository.PersonSpecifications.AgeBetweenSpec" project="repository"
23+
file="/repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java" binary="false"
24+
corner="BOTTOM_RIGHT">
25+
<position height="-1" width="-1" x="412" y="246"/>
26+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
27+
sort-features="false" accessors="true" visibility="true">
28+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
29+
<operations public="true" package="true" protected="true" private="true" static="true"/>
30+
</display>
31+
</class>
32+
<class id="4" language="java" name="com.iluwatar.repository.PersonSpecifications" project="repository"
33+
file="/repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java" binary="false"
34+
corner="BOTTOM_RIGHT">
35+
<position height="-1" width="-1" x="627" y="68"/>
36+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
37+
sort-features="false" accessors="true" visibility="true">
38+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
39+
<operations public="true" package="true" protected="true" private="true" static="true"/>
40+
</display>
41+
</class>
42+
<class id="5" language="java" name="com.iluwatar.repository.PersonSpecifications.NameEqualSpec" project="repository"
43+
file="/repository/src/main/java/com/iluwatar/repository/PersonSpecifications.java" binary="false"
44+
corner="BOTTOM_RIGHT">
45+
<position height="-1" width="-1" x="834" y="238"/>
46+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
47+
sort-features="false" accessors="true" visibility="true">
48+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
49+
<operations public="true" package="true" protected="true" private="true" static="true"/>
50+
</display>
51+
</class>
52+
<nesting id="6">
53+
<end type="SOURCE" refId="4"/>
54+
<end type="TARGET" refId="3"/>
55+
</nesting>
56+
<dependency id="7">
2357
<end type="SOURCE" refId="2"/>
2458
<end type="TARGET" refId="1"/>
2559
</dependency>
60+
<nesting id="8">
61+
<end type="SOURCE" refId="4"/>
62+
<end type="TARGET" refId="5"/>
63+
</nesting>
2664
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
2765
sort-features="false" accessors="true" visibility="true">
2866
<attributes public="true" package="true" protected="true" private="true" static="true"/>

repository/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ querying is utilized.
2525
**Real world examples:**
2626

2727
* [Spring Data](http://projects.spring.io/spring-data/)
28+
29+
**Credits:**
30+
31+
* [Don’t use DAO, use Repository](http://thinkinginobjects.com/2012/08/26/dont-use-dao-use-repository/)
32+
* [Advanced Spring Data JPA - Specifications and Querydsl](https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/)
33+

repository/pom.xml

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,42 @@
11
<?xml version="1.0"?>
2-
<project
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4-
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5-
<modelVersion>4.0.0</modelVersion>
6-
<parent>
7-
<groupId>com.iluwatar</groupId>
8-
<artifactId>java-design-patterns</artifactId>
9-
<version>1.8.0-SNAPSHOT</version>
10-
</parent>
11-
<artifactId>repository</artifactId>
12-
<dependencies>
13-
<dependency>
14-
<groupId>org.springframework.data</groupId>
15-
<artifactId>spring-data-jpa</artifactId>
16-
</dependency>
17-
<dependency>
18-
<groupId>org.hibernate</groupId>
19-
<artifactId>hibernate-entitymanager</artifactId>
20-
</dependency>
21-
<dependency>
22-
<groupId>commons-dbcp</groupId>
23-
<artifactId>commons-dbcp</artifactId>
24-
</dependency>
25-
<dependency>
26-
<groupId>com.h2database</groupId>
27-
<artifactId>h2</artifactId>
28-
</dependency>
29-
<dependency>
30-
<groupId>junit</groupId>
31-
<artifactId>junit</artifactId>
32-
<scope>test</scope>
33-
</dependency>
34-
</dependencies>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
3+
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.8.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>repository</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.springframework</groupId>
14+
<artifactId>spring-test</artifactId>
15+
</dependency>
16+
<dependency>
17+
<groupId>org.springframework.data</groupId>
18+
<artifactId>spring-data-jpa</artifactId>
19+
</dependency>
20+
<dependency>
21+
<groupId>org.hibernate</groupId>
22+
<artifactId>hibernate-entitymanager</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>commons-dbcp</groupId>
26+
<artifactId>commons-dbcp</artifactId>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.h2database</groupId>
30+
<artifactId>h2</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>junit</groupId>
34+
<artifactId>junit</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>com.google.guava</groupId>
39+
<artifactId>guava</artifactId>
40+
</dependency>
41+
</dependencies>
3542
</project>

repository/src/main/java/com/iluwatar/repository/App.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import org.springframework.context.support.ClassPathXmlApplicationContext;
66

77
/**
8-
*
98
* Repository pattern mediates between the domain and data mapping layers using a collection-like
109
* interface for accessing domain objects. A system with complex domain model often benefits from a
1110
* layer that isolates domain objects from the details of the database access code and in such
@@ -16,9 +15,9 @@
1615
* <p>
1716
* In this example we utilize Spring Data to automatically generate a repository for us from the
1817
* {@link Person} domain object. Using the {@link PersonRepository} we perform CRUD operations on
19-
* the entity. Underneath we have configured in-memory H2 database for which schema is created and
20-
* dropped on each run.
21-
*
18+
* the entity, moreover, the query by {@link org.springframework.data.jpa.domain.Specification} are
19+
* also performed. Underneath we have configured in-memory H2 database for which schema is created
20+
* and dropped on each run.
2221
*/
2322
public class App {
2423

@@ -28,16 +27,21 @@ public class App {
2827
* @param args command line args
2928
*/
3029
public static void main(String[] args) {
30+
3131
ClassPathXmlApplicationContext context =
3232
new ClassPathXmlApplicationContext("applicationContext.xml");
3333
PersonRepository repository = context.getBean(PersonRepository.class);
3434

35-
Person peter = new Person("Peter", "Sagan");
36-
Person nasta = new Person("Nasta", "Kuzminova");
35+
Person peter = new Person("Peter", "Sagan", 17);
36+
Person nasta = new Person("Nasta", "Kuzminova", 25);
37+
Person john = new Person("John", "lawrence", 35);
38+
Person terry = new Person("Terry", "Law", 36);
3739

3840
// Add new Person records
3941
repository.save(peter);
4042
repository.save(nasta);
43+
repository.save(john);
44+
repository.save(terry);
4145

4246
// Count Person records
4347
System.out.println("Count Person records: " + repository.count());
@@ -48,9 +52,6 @@ public static void main(String[] args) {
4852
System.out.println(person);
4953
}
5054

51-
// Find Person by surname
52-
System.out.println("Find by surname 'Sagan': " + repository.findBySurname("Sagan"));
53-
5455
// Update Person
5556
nasta.setName("Barbora");
5657
nasta.setSurname("Spotakova");
@@ -61,9 +62,22 @@ public static void main(String[] args) {
6162
// Remove record from Person
6263
repository.delete(2L);
6364

64-
// And finally count records
65+
// count records
6566
System.out.println("Count Person records: " + repository.count());
6667

68+
// find by name
69+
Person p = repository.findOne(new PersonSpecifications.NameEqualSpec("John"));
70+
System.out.println("Find by John is " + p);
71+
72+
// find by age
73+
persons = repository.findAll(new PersonSpecifications.AgeBetweenSpec(20, 40));
74+
75+
System.out.println("Find Person with age between 20,40: ");
76+
for (Person person : persons) {
77+
System.out.println(person);
78+
}
79+
6780
context.close();
81+
6882
}
6983
}

repository/src/main/java/com/iluwatar/repository/Person.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ public class Person {
1818
private String name;
1919
private String surname;
2020

21+
private int age;
22+
2123
public Person() {}
2224

23-
public Person(String name, String surname) {
25+
public Person(String name, String surname, int age) {
2426
this.name = name;
2527
this.surname = surname;
28+
this.age = age;
2629
}
2730

2831
public Long getId() {
@@ -49,8 +52,59 @@ public void setSurname(String surname) {
4952
this.surname = surname;
5053
}
5154

55+
56+
public int getAge() {
57+
return age;
58+
}
59+
60+
public void setAge(int age) {
61+
this.age = age;
62+
}
63+
5264
@Override
5365
public String toString() {
54-
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + "]";
66+
return "Person [id=" + id + ", name=" + name + ", surname=" + surname + ", age=" + age + "]";
67+
}
68+
69+
@Override
70+
public int hashCode() {
71+
72+
final int prime = 31;
73+
int result = 1;
74+
result = prime * result + age;
75+
result = prime * result + ((id == null) ? 0 : id.hashCode());
76+
result = prime * result + ((name == null) ? 0 : name.hashCode());
77+
result = prime * result + ((surname == null) ? 0 : surname.hashCode());
78+
return result;
5579
}
80+
81+
@Override
82+
public boolean equals(Object obj) {
83+
if (this == obj)
84+
return true;
85+
if (obj == null)
86+
return false;
87+
if (getClass() != obj.getClass())
88+
return false;
89+
Person other = (Person) obj;
90+
if (age != other.age)
91+
return false;
92+
if (id == null) {
93+
if (other.id != null)
94+
return false;
95+
} else if (!id.equals(other.id))
96+
return false;
97+
if (name == null) {
98+
if (other.name != null)
99+
return false;
100+
} else if (!name.equals(other.name))
101+
return false;
102+
if (surname == null) {
103+
if (other.surname != null)
104+
return false;
105+
} else if (!surname.equals(other.surname))
106+
return false;
107+
return true;
108+
}
109+
56110
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.iluwatar.repository;
22

3-
import java.util.List;
4-
3+
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
54
import org.springframework.data.repository.CrudRepository;
65
import org.springframework.stereotype.Repository;
76

@@ -11,7 +10,8 @@
1110
*
1211
*/
1312
@Repository
14-
public interface PersonRepository extends CrudRepository<Person, Long> {
13+
public interface PersonRepository
14+
extends CrudRepository<Person, Long>, JpaSpecificationExecutor<Person> {
1515

16-
public List<Person> findBySurname(String surname);
16+
public Person findByName(String name);
1717
}

0 commit comments

Comments
 (0)