Skip to content

Commit b805e21

Browse files
committed
#153 - Create example module for Query by Example with Spring Data JPA.
1 parent bb833a2 commit b805e21

File tree

10 files changed

+300
-0
lines changed

10 files changed

+300
-0
lines changed

jpa/pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<module>security</module>
2626
<module>multiple-datasources</module>
2727
<module>eclipselink</module>
28+
<module>query-by-example</module>
2829
</modules>
2930

3031
<dependencies>

jpa/query-by-example/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Spring Data JPA - Query-by-Example (QBE) example
2+
3+
This project contains samples of Query-by-Example of Spring Data JPA.
4+
5+
## Support for Query-by-Example
6+
7+
Query by Example (QBE) is a user-friendly querying technique with a simple interface. It allows dynamic query creation and does not require to write queries containing field names. In fact, Query by Example does not require to write queries using JPA-QL at all.
8+
9+
An `Example` takes a data object (usually the entity object or a subtype of it) and a specification how to match properties. You can use Query by Example with JPA Repositories.
10+
11+
This example contains a test class to illustrate Query-by-Example with a Repository in `UserRepositoryIntegrationTests`.

jpa/query-by-example/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>org.springframework.data.examples</groupId>
7+
<artifactId>spring-data-jpa-examples</artifactId>
8+
<version>1.0.0.BUILD-SNAPSHOT</version>
9+
</parent>
10+
11+
<artifactId>spring-data-jpa-query-by-example</artifactId>
12+
<name>Spring Data JPA - Query-by-Example (QBE)</name>
13+
14+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.jpa.querybyexample;
17+
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;
22+
23+
/**
24+
* @author Mark Paluch
25+
*/
26+
@Configuration
27+
@EnableAutoConfiguration
28+
@EntityScan(basePackageClasses = { ApplicationConfiguration.class })
29+
@EnableJpaAuditing
30+
public class ApplicationConfiguration {
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.jpa.querybyexample;
17+
18+
import javax.persistence.Entity;
19+
import javax.persistence.GeneratedValue;
20+
import javax.persistence.Id;
21+
22+
import lombok.Data;
23+
24+
/**
25+
* Sample user class.
26+
*
27+
* @author Mark Paluch
28+
*/
29+
@Entity
30+
@Data
31+
public class User {
32+
33+
@Id @GeneratedValue //
34+
private Long id;
35+
private String firstname;
36+
private String lastname;
37+
private Integer age;
38+
39+
public User() {
40+
super();
41+
}
42+
43+
public User(String firstname, String lastname, Integer age) {
44+
45+
super();
46+
47+
this.firstname = firstname;
48+
this.lastname = lastname;
49+
this.age = age;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.jpa.querybyexample;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
import org.springframework.data.repository.query.QueryByExampleExecutor;
20+
21+
/**
22+
* Simple repository interface for {@link User} instances. The interface implements {@link QueryByExampleExecutor} and
23+
* allows execution of methods accepting {@link org.springframework.data.domain.Example}.
24+
*
25+
* @author Mark Paluch
26+
*/
27+
public interface UserRepository extends CrudRepository<User, Long>, QueryByExampleExecutor<User> {
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Sample showing Query-by-Example related features of Spring Data JPA.
3+
*
4+
* @author Mark Paluch
5+
*/
6+
package example.springdata.jpa.querybyexample;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.datasource.separator=/;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
5+
<encoder>
6+
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="error" />
11+
12+
<root level="error">
13+
<appender-ref ref="console" />
14+
</root>
15+
16+
</configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package example.springdata.jpa.querybyexample;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
21+
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.test.SpringApplicationConfiguration;
27+
import org.springframework.data.domain.Example;
28+
import org.springframework.data.domain.ExampleSpec;
29+
import org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers;
30+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31+
import org.springframework.transaction.annotation.Transactional;
32+
33+
/**
34+
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
35+
*
36+
* @author Mark Paluch
37+
*/
38+
@RunWith(SpringJUnit4ClassRunner.class)
39+
@Transactional
40+
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
41+
public class UserRepositoryIntegrationTests {
42+
43+
@Autowired UserRepository repository;
44+
45+
User skyler, walter, flynn, marie, hank;
46+
47+
@Before
48+
public void setUp() {
49+
50+
repository.deleteAll();
51+
52+
this.skyler = repository.save(new User("Skyler", "White", 45));
53+
this.walter = repository.save(new User("Walter", "White", 50));
54+
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
55+
this.marie = repository.save(new User("Marie", "Schrader", 38));
56+
this.hank = repository.save(new User("Hank", "Schrader", 43));
57+
}
58+
59+
/**
60+
* @see DATAJPA-218
61+
*/
62+
@Test
63+
public void countBySimpleExample() {
64+
65+
Example<User> example = Example.of(new User(null, "White", null));
66+
67+
assertThat(repository.count(example), is(3L));
68+
}
69+
70+
/**
71+
* @see DATAJPA-218
72+
*/
73+
@Test
74+
public void ignorePropertiesAndMatchByAge() {
75+
76+
Example<User> example = ExampleSpec.of(User.class). //
77+
withIgnorePaths("firstname", "lastname").//
78+
createExample(flynn);
79+
80+
assertThat(repository.findOne(example), is(flynn));
81+
}
82+
83+
/**
84+
* @see DATAJPA-218
85+
*/
86+
@Test
87+
public void substringMatching() {
88+
89+
Example<User> example = ExampleSpec.of(User.class).//
90+
withStringMatcherEnding()//
91+
.createExample(new User("er", null, null));
92+
93+
assertThat(repository.findAll(example), hasItems(skyler, walter));
94+
}
95+
96+
/**
97+
* @see DATAJPA-218
98+
*/
99+
@Test
100+
public void matchStartingStringsIgnoreCase() {
101+
102+
Example<User> example = ExampleSpec.of(User.class). //
103+
withIgnorePaths("age").//
104+
withMatcher("firstname", startsWith()).//
105+
withMatcher("lastname", ignoreCase()).//
106+
createExample(new User("Walter", "WHITE", null));
107+
108+
assertThat(repository.findAll(example), hasItems(flynn, walter));
109+
}
110+
111+
/**
112+
* @see DATAJPA-218
113+
*/
114+
@Test
115+
public void configuringMatchersUsingLambdas() {
116+
117+
Example<User> example = ExampleSpec.of(User.class).withIgnorePaths("age"). //
118+
withMatcher("firstname", matcher -> matcher.startsWith()). //
119+
withMatcher("lastname", matcher -> matcher.ignoreCase()). //
120+
createExample(new User("Walter", "WHITE", null));
121+
122+
assertThat(repository.findAll(example), hasItems(flynn, walter));
123+
}
124+
125+
/**
126+
* @see DATAJPA-218
127+
*/
128+
@Test
129+
public void valueTransformer() {
130+
131+
Example<User> example = ExampleSpec.of(User.class). //
132+
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50))).//
133+
createExample(new User(null, "White", 99));
134+
135+
assertThat(repository.findAll(example), hasItems(walter));
136+
}
137+
138+
}

0 commit comments

Comments
 (0)