Skip to content

Create example module for Query by Example for JPA and MongoDB #154

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<module>security</module>
<module>multiple-datasources</module>
<module>eclipselink</module>
<module>query-by-example</module>
</modules>

<dependencies>
Expand Down
11 changes: 11 additions & 0 deletions jpa/query-by-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Spring Data JPA - Query-by-Example (QBE) example

This project contains samples of Query-by-Example of Spring Data JPA.

## Support for Query-by-Example

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.

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.

This example contains a test class to illustrate Query-by-Example with a Repository in `UserRepositoryIntegrationTests`.
14 changes: 14 additions & 0 deletions jpa/query-by-example/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-jpa-examples</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
</parent>

<artifactId>spring-data-jpa-query-by-example</artifactId>
<name>Spring Data JPA - Query-by-Example (QBE)</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jpa.querybyexample;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

/**
* @author Mark Paluch
*/
@Configuration
@EnableAutoConfiguration
@EntityScan(basePackageClasses = { ApplicationConfiguration.class })
@EnableJpaAuditing
public class ApplicationConfiguration {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.springdata.jpa.querybyexample;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;

/**
* Sample class that extends {@link User}.
*
* @author Mark Paluch
*/
@Entity
@Data
@NoArgsConstructor
public class SpecialUser extends User {

public SpecialUser(String firstname, String lastname, Integer age) {
super(firstname, lastname, age);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jpa.querybyexample;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Sample user class.
*
* @author Mark Paluch
*/
@Entity
@Data
@NoArgsConstructor
public class User {

@Id @GeneratedValue //
private Long id;
private String firstname;
private String lastname;
private Integer age;

public User(String firstname, String lastname, Integer age) {

super();

this.firstname = firstname;
this.lastname = lastname;
this.age = age;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.jpa.querybyexample;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.QueryByExampleExecutor;

/**
* Simple repository interface for {@link User} instances. The interface implements {@link QueryByExampleExecutor} and
* allows execution of methods accepting {@link org.springframework.data.domain.Example}.
*
* @author Mark Paluch
*/
public interface UserRepository extends CrudRepository<User, Long>, QueryByExampleExecutor<User> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Sample showing Query-by-Example related features of Spring Data JPA.
*
* @author Mark Paluch
*/
package example.springdata.jpa.querybyexample;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.datasource.separator=/;
16 changes: 16 additions & 0 deletions jpa/query-by-example/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d %5p %40.40c:%4L - %m%n</pattern>
</encoder>
</appender>

<logger name="org.springframework" level="error" />

<root level="error">
<appender-ref ref="console" />
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package example.springdata.jpa.querybyexample;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.*;
import static org.springframework.data.domain.ExampleSpec.GenericPropertyMatchers.startsWith;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleSpec;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
* Integration test showing the usage of JPA Query-by-Example support through Spring Data repositories.
*
* @author Mark Paluch
*/
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
public class UserRepositoryInheritanceIntegrationTests {

@Autowired UserRepository repository;

User skyler, walter, flynn;

@Before
public void setUp() {

repository.deleteAll();

this.skyler = repository.save(new User("Skyler", "White", 45));
this.walter = repository.save(new SpecialUser("Walter", "White", 50));
this.flynn = repository.save(new SpecialUser("Walter Jr. (Flynn)", "White", 17));
}

/**
* @see DATAJPA-218
*/
@Test
public void countBySimpleExample() {

Example<User> example = Example.of(new SpecialUser(null, "White", null));

assertThat(repository.count(example), is(3L));
}

/**
* @see DATAJPA-218
*/
@Test
public void countUserByTypedExample() {

Example<User> example = Example.of(new SpecialUser(null, "White", null), //
ExampleSpec.typed(User.class));

assertThat(repository.count(example), is(3L));
}

/**
* @see DATAJPA-218
*/
@Test
public void countSpecialUserByTypedExample() {

Example<SpecialUser> example = Example.of(new SpecialUser(null, "White", null), //
ExampleSpec.typed(SpecialUser.class));

assertThat(repository.count(example), is(2L));
}

}
Loading