55import 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
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 */
2322public 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}
0 commit comments