#149 - Polishing.
Renamed repository folder to repositories. Tweaked package name to reflect module name. Polished formatting in readme. Switched to Spring Boot version property for 1.7 RC1 upgrade. Original pull request: #162.
This commit is contained in:
@@ -20,7 +20,7 @@
|
||||
<module>cluster-sentinel</module>
|
||||
<module>example</module>
|
||||
<module>cluster</module>
|
||||
<module>repository</module>
|
||||
<module>repositories</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
||||
@@ -10,15 +10,15 @@ Redis Repository support allows to convert, store, retrieve and index entities w
|
||||
@RedisHash("persons")
|
||||
class Person {
|
||||
|
||||
@Id String id;
|
||||
|
||||
@Indexed String firstname;
|
||||
@Indexed String lastname;
|
||||
@Id String id;
|
||||
|
||||
Gender gender;
|
||||
Address address;
|
||||
@Indexed String firstname;
|
||||
@Indexed String lastname;
|
||||
|
||||
@Reference List<Person> children;
|
||||
Gender gender;
|
||||
Address address;
|
||||
|
||||
@Reference List<Person> children;
|
||||
}
|
||||
```
|
||||
|
||||
@@ -81,36 +81,36 @@ The below configuration uses [Jedis](https://github.com/xetorthio/jedis) to conn
|
||||
@EnableRedisRepositories
|
||||
class AppConfig {
|
||||
|
||||
@Bean
|
||||
RedisConnectionFactory connectionFactory() {
|
||||
return new JedisConnectionFactory();
|
||||
}
|
||||
@Bean
|
||||
RedisConnectionFactory connectionFactory() {
|
||||
return new JedisConnectionFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
@Bean
|
||||
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
|
||||
RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
RedisTemplate<byte[], byte[]> template = new RedisTemplate<byte[], byte[]>();
|
||||
template.setConnectionFactory(connectionFactory);
|
||||
|
||||
return template;
|
||||
}
|
||||
return template;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Having the infrastructure in place you can go on declaring and using the `Repository` interface.
|
||||
Having the infrastructure in place you can go on declaring and using the `Repository` interface.
|
||||
|
||||
```java
|
||||
interface PersonRepository extends CrudRepository<Person, String> {
|
||||
|
||||
List<Person> findByLastname(String lastname);
|
||||
List<Person> findByLastname(String lastname);
|
||||
|
||||
Page<Person> findByLastname(String lastname, Pageable page);
|
||||
Page<Person> findByLastname(String lastname, Pageable page);
|
||||
|
||||
List<Person> findByFirstnameAndLastname(String firstname, String lastname);
|
||||
List<Person> findByFirstnameAndLastname(String firstname, String lastname);
|
||||
|
||||
List<Person> findByFirstnameOrLastname(String firstname, String lastname);
|
||||
List<Person> findByFirstnameOrLastname(String firstname, String lastname);
|
||||
|
||||
List<Person> findByAddress_City(String city);
|
||||
List<Person> findByAddress_City(String city);
|
||||
}
|
||||
```
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
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>
|
||||
|
||||
<artifactId>spring-data-redis-repository-example</artifactId>
|
||||
<artifactId>spring-data-redis-repositories-example</artifactId>
|
||||
<name>Spring Data Redis - Repository Support Example</name>
|
||||
|
||||
<parent>
|
||||
@@ -12,39 +12,31 @@
|
||||
<relativePath>../pom.xml</relativePath>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<spring-data-releasetrain.version>Hopper-RC1</spring-data-releasetrain.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>${project.groupId}</groupId>
|
||||
<artifactId>spring-data-redis-example-utils</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.github.kstyrc</groupId>
|
||||
<artifactId>embedded-redis</artifactId>
|
||||
<version>0.6</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<version>1.7.0.RC1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-keyvalue</artifactId>
|
||||
<version>1.1.0.RC1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-commons</artifactId>
|
||||
<version>1.12.0.RC1</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.redis.domain;
|
||||
package example.springdata.redis.repositories;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.redis;
|
||||
package example.springdata.redis.repositories;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -27,22 +27,13 @@ import org.springframework.data.redis.repository.configuration.EnableRedisReposi
|
||||
*/
|
||||
@Configuration
|
||||
@EnableRedisRepositories
|
||||
public class AppConfig {
|
||||
public class ApplicationConfiguration {
|
||||
|
||||
/**
|
||||
* {@link RedisConnectionFactory} to be used when connecting to redis.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
RedisConnectionFactory connectionFactory() {
|
||||
return new JedisConnectionFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param connectionFactory
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
RedisTemplate<?, ?> redisTemplate(RedisConnectionFactory connectionFactory) {
|
||||
|
||||
@@ -51,5 +42,4 @@ public class AppConfig {
|
||||
|
||||
return template;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.redis.domain;
|
||||
package example.springdata.redis.repositories;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -13,13 +13,14 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.redis.domain;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
package example.springdata.redis.repositories;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.annotation.Reference;
|
||||
@@ -54,10 +55,12 @@ import org.springframework.data.redis.core.index.Indexed;
|
||||
@Data
|
||||
@EqualsAndHashCode(exclude = { "children" })
|
||||
@RedisHash("persons")
|
||||
@NoArgsConstructor
|
||||
class Person {
|
||||
|
||||
/**
|
||||
* The {@literal id} and {@link RedisHash#toString()} build up the {@literal key} for the Redis {@literal HASH}. <br />
|
||||
* The {@literal id} and {@link RedisHash#toString()} build up the {@literal key} for the Redis {@literal HASH}.
|
||||
* <br />
|
||||
*
|
||||
* <pre>
|
||||
* <code>
|
||||
@@ -106,12 +109,10 @@ class Person {
|
||||
*/
|
||||
private @Reference List<Person> children;
|
||||
|
||||
public Person() {}
|
||||
|
||||
public Person(String firstname, String lastname, Gender gender) {
|
||||
|
||||
this.firstname = firstname;
|
||||
this.lastname = lastname;
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.redis.domain;
|
||||
package example.springdata.redis.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -35,5 +35,4 @@ interface PersonRepository extends CrudRepository<Person, String> {
|
||||
List<Person> findByFirstnameOrLastname(String firstname, String lastname);
|
||||
|
||||
List<Person> findByAddress_City(String city);
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package example.springdata.redis.domain;
|
||||
package example.springdata.redis.repositories;
|
||||
|
||||
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.*;
|
||||
import static org.hamcrest.core.Is.*;
|
||||
@@ -21,6 +21,9 @@ import static org.hamcrest.core.IsCollectionContaining.*;
|
||||
import static org.hamcrest.core.IsNot.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import example.springdata.redis.test.util.EmbeddedRedisServer;
|
||||
import example.springdata.redis.test.util.RequiresRedisServer;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -41,15 +44,12 @@ import org.springframework.data.redis.core.RedisOperations;
|
||||
import org.springframework.data.redis.core.index.Indexed;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import example.springdata.redis.AppConfig;
|
||||
import example.springdata.redis.test.util.EmbeddedRedisServer;
|
||||
import example.springdata.redis.test.util.RequiresRedisServer;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = AppConfig.class)
|
||||
@SpringApplicationConfiguration(classes = ApplicationConfiguration.class)
|
||||
public class PersonRepositoryTests<K, V> {
|
||||
|
||||
/**
|
||||
@@ -57,14 +57,14 @@ public class PersonRepositoryTests<K, V> {
|
||||
* 1) Start/Stop an embedded instance or reuse an already running local installation <br />
|
||||
* 2) Ignore tests if startup failed and no server running locally.
|
||||
*/
|
||||
public static @ClassRule RuleChain rules = RuleChain.outerRule(
|
||||
EmbeddedRedisServer.runningAt(6379).suppressExceptions()).around(RequiresRedisServer.onLocalhost());
|
||||
public static @ClassRule RuleChain rules = RuleChain
|
||||
.outerRule(EmbeddedRedisServer.runningAt(6379).suppressExceptions()).around(RequiresRedisServer.onLocalhost());
|
||||
|
||||
/** {@link Charset} for String conversion **/
|
||||
private static final Charset CHARSET = Charset.forName("UTF-8");
|
||||
|
||||
@Autowired RedisOperations<K, V> ops;
|
||||
@Autowired PersonRepository repo;
|
||||
@Autowired RedisOperations<K, V> operations;
|
||||
@Autowired PersonRepository repository;
|
||||
|
||||
/*
|
||||
* Set of test users
|
||||
@@ -81,7 +81,7 @@ public class PersonRepositoryTests<K, V> {
|
||||
@After
|
||||
public void setUp() {
|
||||
|
||||
ops.execute((RedisConnection connection) -> {
|
||||
operations.execute((RedisConnection connection) -> {
|
||||
connection.flushDb();
|
||||
return "OK";
|
||||
});
|
||||
@@ -94,10 +94,11 @@ public class PersonRepositoryTests<K, V> {
|
||||
@Test
|
||||
public void saveSingleEntity() {
|
||||
|
||||
repo.save(eddard);
|
||||
repository.save(eddard);
|
||||
|
||||
assertThat(ops.execute((RedisConnection connection) -> connection.exists(new String("persons:" + eddard.getId())
|
||||
.getBytes(CHARSET))), is(true));
|
||||
assertThat(operations.execute(
|
||||
(RedisConnection connection) -> connection.exists(new String("persons:" + eddard.getId()).getBytes(CHARSET))),
|
||||
is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +109,7 @@ public class PersonRepositoryTests<K, V> {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<Person> starks = repo.findByLastname(eddard.getLastname());
|
||||
List<Person> starks = repository.findByLastname(eddard.getLastname());
|
||||
|
||||
assertThat(starks, containsInAnyOrder(eddard, robb, sansa, arya, bran, rickon));
|
||||
assertThat(starks, not(hasItem(jon)));
|
||||
@@ -122,7 +123,7 @@ public class PersonRepositoryTests<K, V> {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<Person> aryaStark = repo.findByFirstnameAndLastname(arya.getFirstname(), arya.getLastname());
|
||||
List<Person> aryaStark = repository.findByFirstnameAndLastname(arya.getFirstname(), arya.getLastname());
|
||||
|
||||
assertThat(aryaStark, hasItem(arya));
|
||||
assertThat(aryaStark, not(hasItems(eddard, robb, sansa, bran, rickon, jon)));
|
||||
@@ -136,7 +137,7 @@ public class PersonRepositoryTests<K, V> {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<Person> aryaAndJon = repo.findByFirstnameOrLastname(arya.getFirstname(), jon.getLastname());
|
||||
List<Person> aryaAndJon = repository.findByFirstnameOrLastname(arya.getFirstname(), jon.getLastname());
|
||||
|
||||
assertThat(aryaAndJon, containsInAnyOrder(arya, jon));
|
||||
assertThat(aryaAndJon, not(hasItems(eddard, robb, sansa, bran, rickon)));
|
||||
@@ -150,12 +151,12 @@ public class PersonRepositoryTests<K, V> {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
Page<Person> page1 = repo.findPersonByLastname(eddard.getLastname(), new PageRequest(0, 5));
|
||||
Page<Person> page1 = repository.findPersonByLastname(eddard.getLastname(), new PageRequest(0, 5));
|
||||
|
||||
assertThat(page1.getNumberOfElements(), is(5));
|
||||
assertThat(page1.getTotalElements(), is(6L));
|
||||
|
||||
Page<Person> page2 = repo.findPersonByLastname(eddard.getLastname(), new PageRequest(1, 5));
|
||||
Page<Person> page2 = repository.findPersonByLastname(eddard.getLastname(), new PageRequest(1, 5));
|
||||
|
||||
assertThat(page2.getNumberOfElements(), is(1));
|
||||
assertThat(page2.getTotalElements(), is(6L));
|
||||
@@ -175,7 +176,7 @@ public class PersonRepositoryTests<K, V> {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
List<Person> eddardStark = repo.findByAddress_City(winterfell.getCity());
|
||||
List<Person> eddardStark = repository.findByAddress_City(winterfell.getCity());
|
||||
|
||||
assertThat(eddardStark, hasItem(eddard));
|
||||
assertThat(eddardStark, not(hasItems(robb, sansa, arya, bran, rickon, jon)));
|
||||
@@ -192,9 +193,9 @@ public class PersonRepositoryTests<K, V> {
|
||||
|
||||
eddard.setChildren(Arrays.asList(jon, robb, sansa, arya, bran, rickon));
|
||||
|
||||
repo.save(eddard);
|
||||
repository.save(eddard);
|
||||
|
||||
Person laoded = repo.findOne(eddard.getId());
|
||||
Person laoded = repository.findOne(eddard.getId());
|
||||
assertThat(laoded.getChildren(), hasItems(jon, robb, sansa, arya, bran, rickon));
|
||||
|
||||
/*
|
||||
@@ -203,14 +204,14 @@ public class PersonRepositoryTests<K, V> {
|
||||
* - Robb was killed by Roose Bolton during the Red Wedding.
|
||||
* - Jon was stabbed by brothers or the Night's Watch.
|
||||
*/
|
||||
repo.delete(Arrays.asList(robb, jon));
|
||||
repository.delete(Arrays.asList(robb, jon));
|
||||
|
||||
laoded = repo.findOne(eddard.getId());
|
||||
laoded = repository.findOne(eddard.getId());
|
||||
assertThat(laoded.getChildren(), hasItems(sansa, arya, bran, rickon));
|
||||
assertThat(laoded.getChildren(), not(hasItems(robb, jon)));
|
||||
}
|
||||
|
||||
private void flushTestUsers() {
|
||||
repo.save(Arrays.asList(eddard, robb, sansa, arya, bran, rickon, jon));
|
||||
repository.save(Arrays.asList(eddard, robb, sansa, arya, bran, rickon, jon));
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
<?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>
|
||||
|
||||
<root level="warn">
|
||||
<appender-ref ref="console" />
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
@@ -10,7 +10,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>spring-data-redis-example-utils</artifactId>
|
||||
<name>Spring Data Redis - Example utilities</name>
|
||||
<name>Spring Data Redis - Example Utilities</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
||||
Reference in New Issue
Block a user