Skip to content

Commit beabdf8

Browse files
mp911deodrotbohm
authored andcommitted
#153 - Add examples for Query by Example functionality with JPA and MongoDB.
Original pull request: #154.
1 parent 1fe9aae commit beabdf8

File tree

25 files changed

+1062
-0
lines changed

25 files changed

+1062
-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,37 @@
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+
17+
package example.springdata.jpa.querybyexample;
18+
19+
import lombok.Data;
20+
import lombok.NoArgsConstructor;
21+
22+
import javax.persistence.Entity;
23+
24+
/**
25+
* Sample class that extends {@link User}.
26+
*
27+
* @author Mark Paluch
28+
*/
29+
@Entity
30+
@Data
31+
@NoArgsConstructor
32+
public class SpecialUser extends User {
33+
34+
public SpecialUser(String firstname, String lastname, Integer age) {
35+
super(firstname, lastname, age);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
import lombok.NoArgsConstructor;
24+
25+
/**
26+
* Sample user class.
27+
*
28+
* @author Mark Paluch
29+
*/
30+
@Entity
31+
@Data
32+
@NoArgsConstructor
33+
public class User {
34+
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+
50+
}
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,93 @@
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+
17+
package example.springdata.jpa.querybyexample;
18+
19+
import static org.hamcrest.CoreMatchers.*;
20+
import static org.junit.Assert.*;
21+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
22+
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;
23+
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
import org.junit.runner.RunWith;
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.boot.test.SpringApplicationConfiguration;
29+
import org.springframework.data.domain.Example;
30+
import org.springframework.data.domain.ExampleSpec;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
import org.springframework.transaction.annotation.Transactional;
33+
34+
/**
35+
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
36+
*
37+
* @author Mark Paluch
38+
*/
39+
@RunWith(SpringJUnit4ClassRunner.class)
40+
@Transactional
41+
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
42+
public class UserRepositoryInheritanceIntegrationTests {
43+
44+
@Autowired UserRepository repository;
45+
46+
User skyler, walter, flynn;
47+
48+
@Before
49+
public void setUp() {
50+
51+
repository.deleteAll();
52+
53+
this.skyler = repository.save(new User("Skyler", "White", 45));
54+
this.walter = repository.save(new SpecialUser("Walter", "White", 50));
55+
this.flynn = repository.save(new SpecialUser("Walter Jr. (Flynn)", "White", 17));
56+
}
57+
58+
/**
59+
* @see DATAJPA-218
60+
*/
61+
@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));
67+
}
68+
69+
/**
70+
* @see DATAJPA-218
71+
*/
72+
@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));
79+
}
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+
93+
}

0 commit comments

Comments
 (0)