|
1 |
| -/* |
2 |
| - * DISCLAIMER |
3 |
| - * |
4 |
| - * Copyright 2017 ArangoDB GmbH, Cologne, Germany |
5 |
| - * |
6 |
| - * Licensed under the Apache License, Version 2.0 (the "License"); |
7 |
| - * you may not use this file except in compliance with the License. |
8 |
| - * You may obtain a copy of the License at |
9 |
| - * |
10 |
| - * http://www.apache.org/licenses/LICENSE-2.0 |
11 |
| - * |
12 |
| - * Unless required by applicable law or agreed to in writing, software |
13 |
| - * distributed under the License is distributed on an "AS IS" BASIS, |
14 |
| - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 |
| - * See the License for the specific language governing permissions and |
16 |
| - * limitations under the License. |
17 |
| - * |
18 |
| - * Copyright holder is ArangoDB GmbH, Cologne, Germany |
19 |
| - */ |
20 |
| - |
21 |
| -package com.arangodb.spring.demo.runner; |
22 |
| - |
23 |
| -import org.springframework.beans.factory.annotation.Autowired; |
24 |
| -import org.springframework.boot.CommandLineRunner; |
25 |
| -import org.springframework.context.annotation.ComponentScan; |
26 |
| -import org.springframework.data.domain.Example; |
27 |
| -import org.springframework.data.domain.ExampleMatcher; |
28 |
| - |
29 |
| -import com.arangodb.spring.demo.entity.Character; |
30 |
| -import com.arangodb.spring.demo.repository.CharacterRepository; |
31 |
| - |
32 |
| -/** |
33 |
| - * @author Mark Vollmary |
34 |
| - * |
35 |
| - */ |
36 |
| -@ComponentScan("com.arangodb.spring.demo") |
37 |
| -public class ByExampleRunner implements CommandLineRunner { |
38 |
| - |
39 |
| - @Autowired |
40 |
| - private CharacterRepository repository; |
41 |
| - |
42 |
| - @Override |
43 |
| - public void run(final String... args) throws Exception { |
44 |
| - System.out.println("# Query by example"); |
45 |
| - |
46 |
| - final Character nedStark = new Character("Ned", "Stark", false, 41); |
47 |
| - |
48 |
| - System.out.println(String.format("## Find character which match %s", nedStark)); |
49 |
| - final Character foundNedStark = repository.findOne(Example.of(nedStark)); |
50 |
| - System.out.println(String.format("Found %s", foundNedStark)); |
51 |
| - |
52 |
| - System.out.println("## Find all dead Starks"); |
53 |
| - // because we only care of surname and alive in our entity we have to ignore the other |
54 |
| - // fields in our ExampleMatcher |
55 |
| - final Iterable<Character> allDeadStarks = repository |
56 |
| - .findAll(Example.of(new Character(null, "Stark", false), ExampleMatcher.matchingAll() |
57 |
| - .withMatcher("surname", match -> match.exact()).withIgnorePaths("name", "age"))); |
58 |
| - allDeadStarks.forEach(System.out::println); |
59 |
| - |
60 |
| - System.out.println("## Find all Starks which are 30 years younger than Ned Stark"); |
61 |
| - // instead of changing the age for the Ned Stark entity use a transformer within the ExampleMatcher. |
62 |
| - // Because we are using the entity fetched from the db we have to ignore the field 'id' which isn't null. |
63 |
| - final Iterable<Character> allYoungerStarks = repository.findAll( |
64 |
| - Example.of(foundNedStark, ExampleMatcher.matchingAll().withMatcher("surname", match -> match.exact()) |
65 |
| - .withIgnorePaths("id", "name", "alive").withTransformer("age", age -> ((int) age) - 30))); |
66 |
| - allYoungerStarks.forEach(System.out::println); |
67 |
| - |
68 |
| - System.out.println("## Find all character which surname ends with 'ark' (ignore case)"); |
69 |
| - final Iterable<Character> ark = repository.findAll(Example.of(new Character(null, "ark", false), |
70 |
| - ExampleMatcher.matchingAll().withMatcher("surname", match -> match.endsWith()).withIgnoreCase() |
71 |
| - .withIgnorePaths("name", "alive", "age"))); |
72 |
| - ark.forEach(System.out::println); |
73 |
| - } |
74 |
| - |
75 |
| -} |
| 1 | +/* |
| 2 | + * DISCLAIMER |
| 3 | + * |
| 4 | + * Copyright 2017 ArangoDB GmbH, Cologne, Germany |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | + */ |
| 20 | + |
| 21 | +package com.arangodb.spring.demo.runner; |
| 22 | + |
| 23 | +import java.util.Optional; |
| 24 | + |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.CommandLineRunner; |
| 27 | +import org.springframework.context.annotation.ComponentScan; |
| 28 | +import org.springframework.data.domain.Example; |
| 29 | +import org.springframework.data.domain.ExampleMatcher; |
| 30 | + |
| 31 | +import com.arangodb.spring.demo.entity.Character; |
| 32 | +import com.arangodb.spring.demo.repository.CharacterRepository; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Mark Vollmary |
| 36 | + * |
| 37 | + */ |
| 38 | +@ComponentScan("com.arangodb.spring.demo") |
| 39 | +public class ByExampleRunner implements CommandLineRunner { |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private CharacterRepository repository; |
| 43 | + |
| 44 | + @Override |
| 45 | + public void run(final String... args) throws Exception { |
| 46 | + System.out.println("# Query by example"); |
| 47 | + |
| 48 | + final Character nedStark = new Character("Ned", "Stark", false, 41); |
| 49 | + |
| 50 | + System.out.println(String.format("## Find character which match %s", nedStark)); |
| 51 | + final Character foundNedStark = repository.findOne(Example.of(nedStark)).get(); |
| 52 | + System.out.println(String.format("Found %s", foundNedStark)); |
| 53 | + |
| 54 | + System.out.println("## Find all dead Starks"); |
| 55 | + // because we only care of surname and alive in our entity we have to ignore the other |
| 56 | + // fields in our ExampleMatcher |
| 57 | + final Iterable<Character> allDeadStarks = repository |
| 58 | + .findAll(Example.of(new Character(null, "Stark", false), ExampleMatcher.matchingAll() |
| 59 | + .withMatcher("surname", match -> match.exact()).withIgnorePaths("name", "age"))); |
| 60 | + allDeadStarks.forEach(System.out::println); |
| 61 | + |
| 62 | + System.out.println("## Find all Starks which are 30 years younger than Ned Stark"); |
| 63 | + // instead of changing the age for the Ned Stark entity use a transformer within the ExampleMatcher. |
| 64 | + // Because we are using the entity fetched from the db we have to ignore the field 'id' which isn't null. |
| 65 | + final Iterable<Character> allYoungerStarks = repository.findAll(Example.of(foundNedStark, |
| 66 | + ExampleMatcher.matchingAll().withMatcher("surname", match -> match.exact()) |
| 67 | + .withIgnorePaths("id", "name", "alive") |
| 68 | + .withTransformer("age", age -> Optional.of(((int) age.get()) - 30)))); |
| 69 | + allYoungerStarks.forEach(System.out::println); |
| 70 | + |
| 71 | + System.out.println("## Find all character which surname ends with 'ark' (ignore case)"); |
| 72 | + final Iterable<Character> ark = repository.findAll(Example.of(new Character(null, "ark", false), |
| 73 | + ExampleMatcher.matchingAll().withMatcher("surname", match -> match.endsWith()).withIgnoreCase() |
| 74 | + .withIgnorePaths("name", "alive", "age"))); |
| 75 | + ark.forEach(System.out::println); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments