Skip to content

Commit

Permalink
Merge pull request #305 from hoswey/master
Browse files Browse the repository at this point in the history
Fix #216 Repository vs DAO
  • Loading branch information
iluwatar committed Nov 27, 2015
2 parents 092d48d + 9d4fff6 commit 5bec636
Show file tree
Hide file tree
Showing 12 changed files with 343 additions and 72 deletions.
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>

Expand Down
Binary file modified repository/etc/repository.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 39 additions & 1 deletion repository/etc/repository.ucls
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>
Expand Down
6 changes: 6 additions & 0 deletions repository/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ querying is utilized.
**Real world examples:**

* [Spring Data](http://projects.spring.io/spring-data/)

**Credits:**

* [Don’t 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/)

73 changes: 40 additions & 33 deletions repository/pom.xml
Original file line number Diff line number Diff line change
@@ -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>
34 changes: 24 additions & 10 deletions repository/src/main/java/com/iluwatar/repository/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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
Expand All @@ -16,9 +15,9 @@
* <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 {

Expand All @@ -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());
Expand All @@ -48,9 +52,6 @@ public static void main(String[] args) {
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");
Expand All @@ -61,9 +62,22 @@ public static void main(String[] args) {
// 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();

}
}
58 changes: 56 additions & 2 deletions repository/src/main/java/com/iluwatar/repository/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -49,8 +52,59 @@ public void setSurname(String surname) {
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;
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -11,7 +10,8 @@
*
*/
@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);
}
Loading

0 comments on commit 5bec636

Please sign in to comment.