Skip to content

Commit 1f94bb1

Browse files
committed
#153 - Polishing.
Simplified domain model using Lombok. Moved configuration classes into test source folder. Original pull request: #154.
1 parent beabdf8 commit 1f94bb1

File tree

21 files changed

+311
-338
lines changed

21 files changed

+311
-338
lines changed

jpa/query-by-example/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010

1111
<artifactId>spring-data-jpa-query-by-example</artifactId>
1212
<name>Spring Data JPA - Query-by-Example (QBE)</name>
13+
14+
<properties>
15+
<spring-data-releasetrain.version>Hopper-BUILD-SNAPSHOT</spring-data-releasetrain.version>
16+
</properties>
1317

1418
</project>

jpa/query-by-example/src/main/java/example/springdata/jpa/querybyexample/SpecialUser.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package example.springdata.jpa.querybyexample;
1817

1918
import lombok.Data;
@@ -25,6 +24,7 @@
2524
* Sample class that extends {@link User}.
2625
*
2726
* @author Mark Paluch
27+
* @author Oliver Gierke
2828
*/
2929
@Entity
3030
@Data

jpa/query-by-example/src/main/java/example/springdata/jpa/querybyexample/User.java

+10-19
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,27 @@
1515
*/
1616
package example.springdata.jpa.querybyexample;
1717

18+
import lombok.Data;
19+
import lombok.NoArgsConstructor;
20+
import lombok.RequiredArgsConstructor;
21+
1822
import javax.persistence.Entity;
1923
import javax.persistence.GeneratedValue;
2024
import javax.persistence.Id;
2125

22-
import lombok.Data;
23-
import lombok.NoArgsConstructor;
24-
2526
/**
2627
* Sample user class.
2728
*
2829
* @author Mark Paluch
30+
* @author Oliver Gierke
2931
*/
3032
@Entity
3133
@Data
32-
@NoArgsConstructor
34+
@NoArgsConstructor(force = true)
35+
@RequiredArgsConstructor
3336
public class User {
3437

35-
@Id @GeneratedValue //
36-
private Long id;
37-
private String firstname;
38-
private String lastname;
39-
private Integer age;
40-
41-
public User(String firstname, String lastname, Integer age) {
42-
43-
super();
44-
45-
this.firstname = firstname;
46-
this.lastname = lastname;
47-
this.age = age;
48-
}
49-
38+
private @Id @GeneratedValue Long id;
39+
private final String firstname, lastname;
40+
private final Integer age;
5041
}

jpa/query-by-example/src/main/java/example/springdata/jpa/querybyexample/UserRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,4 @@
2424
*
2525
* @author Mark Paluch
2626
*/
27-
public interface UserRepository extends CrudRepository<User, Long>, QueryByExampleExecutor<User> {
28-
29-
}
27+
public interface UserRepository extends CrudRepository<User, Long>, QueryByExampleExecutor<User> {}

jpa/query-by-example/src/main/resources/application.properties

-1
This file was deleted.

jpa/query-by-example/src/main/resources/logback.xml

-16
This file was deleted.
+4-11
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,11 @@
1515
*/
1616
package example.springdata.jpa.querybyexample;
1717

18-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
19-
import org.springframework.boot.orm.jpa.EntityScan;
20-
import org.springframework.context.annotation.Configuration;
21-
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
18+
import org.springframework.boot.autoconfigure.SpringBootApplication;
2219

2320
/**
2421
* @author Mark Paluch
22+
* @author Oliver Gierke
2523
*/
26-
@Configuration
27-
@EnableAutoConfiguration
28-
@EntityScan(basePackageClasses = { ApplicationConfiguration.class })
29-
@EnableJpaAuditing
30-
public class ApplicationConfiguration {
31-
32-
}
24+
@SpringBootApplication
25+
public class ApplicationConfiguration {}

jpa/query-by-example/src/test/java/example/springdata/jpa/querybyexample/UserRepositoryInheritanceIntegrationTests.java

+9-31
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,26 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package example.springdata.jpa.querybyexample;
1817

1918
import static org.hamcrest.CoreMatchers.*;
2019
import static org.junit.Assert.*;
21-
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
22-
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
2320

2421
import org.junit.Before;
2522
import org.junit.Test;
2623
import org.junit.runner.RunWith;
2724
import org.springframework.beans.factory.annotation.Autowired;
2825
import org.springframework.boot.test.SpringApplicationConfiguration;
2926
import org.springframework.data.domain.Example;
30-
import org.springframework.data.domain.ExampleSpec;
3127
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3228
import org.springframework.transaction.annotation.Transactional;
3329

3430
/**
35-
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
31+
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories and entities
32+
* using inheritance.
3633
*
3734
* @author Mark Paluch
35+
* @author Oliver Gierke
3836
*/
3937
@RunWith(SpringJUnit4ClassRunner.class)
4038
@Transactional
@@ -56,38 +54,18 @@ public void setUp() {
5654
}
5755

5856
/**
59-
* @see DATAJPA-218
57+
* @see #153
6058
*/
6159
@Test
62-
public void countBySimpleExample() {
63-
64-
Example<User> example = Example.of(new SpecialUser(null, "White", null));
65-
66-
assertThat(repository.count(example), is(3L));
60+
public void countByExample() {
61+
assertThat(repository.count(Example.of(new User(null, "White", null))), is(3L));
6762
}
6863

6964
/**
70-
* @see DATAJPA-218
65+
* @see #153
7166
*/
7267
@Test
73-
public void countUserByTypedExample() {
74-
75-
Example<User> example = Example.of(new SpecialUser(null, "White", null), //
76-
ExampleSpec.typed(User.class));
77-
78-
assertThat(repository.count(example), is(3L));
68+
public void countSubtypesByExample() {
69+
assertThat(repository.count(Example.of(new SpecialUser(null, "White", null))), is(2L));
7970
}
80-
81-
/**
82-
* @see DATAJPA-218
83-
*/
84-
@Test
85-
public void countSpecialUserByTypedExample() {
86-
87-
Example<SpecialUser> example = Example.of(new SpecialUser(null, "White", null), //
88-
ExampleSpec.typed(SpecialUser.class));
89-
90-
assertThat(repository.count(example), is(2L));
91-
}
92-
9371
}

jpa/query-by-example/src/test/java/example/springdata/jpa/querybyexample/UserRepositoryIntegrationTests.java

+76-72
Original file line numberDiff line numberDiff line change
@@ -17,118 +17,122 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20-
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
21-
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
20+
import static org.springframework.data.domain.ExampleMatcher.*;
21+
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.*;
22+
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers.startsWith;
2223

2324
import org.junit.Before;
2425
import org.junit.Test;
2526
import org.junit.runner.RunWith;
2627
import org.springframework.beans.factory.annotation.Autowired;
2728
import org.springframework.boot.test.SpringApplicationConfiguration;
2829
import org.springframework.data.domain.Example;
29-
import org.springframework.data.domain.ExampleSpec;
30+
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
3031
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3132
import org.springframework.transaction.annotation.Transactional;
3233

3334
/**
3435
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
3536
*
3637
* @author Mark Paluch
38+
* @author Oliver Gierke
3739
*/
40+
@SuppressWarnings("unused")
3841
@RunWith(SpringJUnit4ClassRunner.class)
3942
@Transactional
4043
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
4144
public class UserRepositoryIntegrationTests {
4245

43-
@Autowired
44-
UserRepository repository;
46+
@Autowired UserRepository repository;
4547

46-
User skyler, walter, flynn, marie, hank;
48+
User skyler, walter, flynn, marie, hank;
4749

48-
@Before
49-
public void setUp() {
50+
@Before
51+
public void setUp() {
5052

51-
repository.deleteAll();
53+
repository.deleteAll();
5254

53-
this.skyler = repository.save(new User("Skyler", "White", 45));
54-
this.walter = repository.save(new User("Walter", "White", 50));
55-
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
56-
this.marie = repository.save(new User("Marie", "Schrader", 38));
57-
this.hank = repository.save(new User("Hank", "Schrader", 43));
58-
}
55+
this.skyler = repository.save(new User("Skyler", "White", 45));
56+
this.walter = repository.save(new User("Walter", "White", 50));
57+
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
58+
this.marie = repository.save(new User("Marie", "Schrader", 38));
59+
this.hank = repository.save(new User("Hank", "Schrader", 43));
60+
}
5961

60-
/**
61-
* @see DATAJPA-218
62-
*/
63-
@Test
64-
public void countBySimpleExample() {
62+
/**
63+
* @see #153
64+
*/
65+
@Test
66+
public void countBySimpleExample() {
6567

66-
Example<User> example = Example.of(new User(null, "White", null));
68+
Example<User> example = Example.of(new User(null, "White", null));
6769

68-
assertThat(repository.count(example), is(3L));
69-
}
70+
assertThat(repository.count(example), is(3L));
71+
}
7072

71-
/**
72-
* @see DATAJPA-218
73-
*/
74-
@Test
75-
public void ignorePropertiesAndMatchByAge() {
73+
/**
74+
* @see #153
75+
*/
76+
@Test
77+
public void ignorePropertiesAndMatchByAge() {
7678

77-
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
78-
withIgnorePaths("firstname", "lastname");
79+
Example<User> example = Example.of(flynn, matching().//
80+
withIgnorePaths("firstname", "lastname"));
7981

80-
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
81-
}
82+
assertThat(repository.findOne(example), is(flynn));
83+
}
8284

83-
/**
84-
* @see DATAJPA-218
85-
*/
86-
@Test
87-
public void substringMatching() {
85+
/**
86+
* @see #153
87+
*/
88+
@Test
89+
public void substringMatching() {
8890

89-
ExampleSpec exampleSpec = ExampleSpec.untyped().//
90-
withStringMatcherEnding();
91+
Example<User> example = Example.of(new User("er", null, null), matching().//
92+
withStringMatcher(StringMatcher.ENDING));
9193

92-
assertThat(repository.findAll(Example.of(new User("er", null, null), exampleSpec)), hasItems(skyler, walter));
93-
}
94+
assertThat(repository.findAll(example), hasItems(skyler, walter));
95+
}
9496

95-
/**
96-
* @see DATAJPA-218
97-
*/
98-
@Test
99-
public void matchStartingStringsIgnoreCase() {
97+
/**
98+
* @see #153
99+
*/
100+
@Test
101+
public void matchStartingStringsIgnoreCase() {
100102

101-
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
102-
withIgnorePaths("age").//
103-
withMatcher("firstname", startsWith()).//
104-
withMatcher("lastname", ignoreCase());
103+
Example<User> example = Example.of(new User("Walter", "WHITE", null),
104+
matching().//
105+
withIgnorePaths("age").//
106+
withMatcher("firstname", startsWith()).//
107+
withMatcher("lastname", ignoreCase()));
105108

106-
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
107-
}
109+
assertThat(repository.findAll(example), hasItems(flynn, walter));
110+
}
108111

109-
/**
110-
* @see DATAJPA-218
111-
*/
112-
@Test
113-
public void configuringMatchersUsingLambdas() {
112+
/**
113+
* @see #153
114+
*/
115+
@Test
116+
public void configuringMatchersUsingLambdas() {
114117

115-
ExampleSpec exampleSpec = ExampleSpec.untyped().withIgnorePaths("age"). //
116-
withMatcher("firstname", matcher -> matcher.startsWith()). //
117-
withMatcher("lastname", matcher -> matcher.ignoreCase());
118+
Example<User> example = Example.of(new User("Walter", "WHITE", null),
119+
matching().//
120+
withIgnorePaths("age").//
121+
withMatcher("firstname", matcher -> matcher.startsWith()).//
122+
withMatcher("lastname", matcher -> matcher.ignoreCase()));
118123

119-
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
120-
}
124+
assertThat(repository.findAll(example), hasItems(flynn, walter));
125+
}
121126

122-
/**
123-
* @see DATAJPA-218
124-
*/
125-
@Test
126-
public void valueTransformer() {
127+
/**
128+
* @see #153
129+
*/
130+
@Test
131+
public void valueTransformer() {
127132

128-
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
129-
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
130-
131-
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
132-
}
133+
Example<User> example = Example.of(new User(null, "White", 99), matching(). //
134+
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))));
133135

136+
assertThat(repository.findAll(example), hasItems(walter));
137+
}
134138
}

0 commit comments

Comments
 (0)