#153 - Add examples for Query by Example functionality with JPA and MongoDB.
Original pull request: #154.
This commit is contained in:
committed by
Oliver Gierke
parent
1fe9aaeefa
commit
beabdf8a79
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
/**
|
||||
* Sample showing Query-by-Example related features of Spring Data JPA.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
package example.springdata.jpa.querybyexample;
|
||||
@@ -0,0 +1 @@
|
||||
spring.datasource.separator=/;
|
||||
16
jpa/query-by-example/src/main/resources/logback.xml
Normal file
16
jpa/query-by-example/src/main/resources/logback.xml
Normal file
@@ -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>
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* 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 UserRepositoryIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
UserRepository repository;
|
||||
|
||||
User skyler, walter, flynn, marie, hank;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
repository.deleteAll();
|
||||
|
||||
this.skyler = repository.save(new User("Skyler", "White", 45));
|
||||
this.walter = repository.save(new User("Walter", "White", 50));
|
||||
this.flynn = repository.save(new User("Walter Jr. (Flynn)", "White", 17));
|
||||
this.marie = repository.save(new User("Marie", "Schrader", 38));
|
||||
this.hank = repository.save(new User("Hank", "Schrader", 43));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-218
|
||||
*/
|
||||
@Test
|
||||
public void countBySimpleExample() {
|
||||
|
||||
Example<User> example = Example.of(new User(null, "White", null));
|
||||
|
||||
assertThat(repository.count(example), is(3L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-218
|
||||
*/
|
||||
@Test
|
||||
public void ignorePropertiesAndMatchByAge() {
|
||||
|
||||
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
|
||||
withIgnorePaths("firstname", "lastname");
|
||||
|
||||
assertThat(repository.findOne(Example.of(flynn, exampleSpec)), is(flynn));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-218
|
||||
*/
|
||||
@Test
|
||||
public void substringMatching() {
|
||||
|
||||
ExampleSpec exampleSpec = ExampleSpec.untyped().//
|
||||
withStringMatcherEnding();
|
||||
|
||||
assertThat(repository.findAll(Example.of(new User("er", null, null), exampleSpec)), hasItems(skyler, walter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-218
|
||||
*/
|
||||
@Test
|
||||
public void matchStartingStringsIgnoreCase() {
|
||||
|
||||
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
|
||||
withIgnorePaths("age").//
|
||||
withMatcher("firstname", startsWith()).//
|
||||
withMatcher("lastname", ignoreCase());
|
||||
|
||||
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-218
|
||||
*/
|
||||
@Test
|
||||
public void configuringMatchersUsingLambdas() {
|
||||
|
||||
ExampleSpec exampleSpec = ExampleSpec.untyped().withIgnorePaths("age"). //
|
||||
withMatcher("firstname", matcher -> matcher.startsWith()). //
|
||||
withMatcher("lastname", matcher -> matcher.ignoreCase());
|
||||
|
||||
assertThat(repository.findAll(Example.of(new User("Walter", "WHITE", null), exampleSpec)), hasItems(flynn, walter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-218
|
||||
*/
|
||||
@Test
|
||||
public void valueTransformer() {
|
||||
|
||||
ExampleSpec exampleSpec = ExampleSpec.untyped(). //
|
||||
withMatcher("age", matcher -> matcher.transform(value -> Integer.valueOf(50)));
|
||||
|
||||
assertThat(repository.findAll(Example.of(new User(null, "White", 99), exampleSpec)), hasItems(walter));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user