#231 - Added example for Javaslang support on repositories.

Added an example that shows how Javaslang's collection types and Option can be used as repository query methods starting with Spring Data Ingalls RC1.
This commit is contained in:
Oliver Gierke
2016-12-14 14:05:38 +01:00
parent ab2ac6d7c4
commit cd36f6f0da
5 changed files with 193 additions and 0 deletions

26
jpa/javaslang/pom.xml Normal file
View File

@@ -0,0 +1,26 @@
<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-javaslang</artifactId>
<name>Spring Data JPA - Javaslang integration</name>
<properties>
<spring-data-releasetrain.version>Ingalls-BUILD-SNAPSHOT</spring-data-releasetrain.version>
</properties>
<dependencies>
<dependency>
<groupId>io.javaslang</groupId>
<artifactId>javaslang</artifactId>
<version>2.0.5</version>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,38 @@
/*
* 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;
import lombok.AccessLevel;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author Oliver Gierke
*/
@Data
@Entity
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@RequiredArgsConstructor
public class Person {
private @GeneratedValue @Id Long id;
private final String firstname, lastname;
}

View File

@@ -0,0 +1,54 @@
/*
* 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;
import javaslang.collection.Map;
import javaslang.collection.Seq;
import javaslang.collection.Set;
import javaslang.control.Option;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.Repository;
/**
* Repository interface showing the usage of Javaslang collections and its {@link Option} type as repository query
* method return types.
*
* @author Oliver Gierke
*/
public interface PersonRepository extends Repository<Person, Long> {
Person save(Person person);
/**
* {@link Option} can be used as alternative to JDK 8's {@link Optional}.
*
* @param id
* @return
*/
Option<Person> findById(Long id);
/**
* {@link Seq} can be used as alternative to JDK's {@link List}. Javaslang's {@link Set} and {@link Map} are
* supported, too, and transparently mapped from their JDK counterparts.
*
* @param firstname
* @return
*/
Seq<Person> findByFirstnameContaining(String firstname);
}

View File

@@ -0,0 +1,74 @@
/*
* 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;
import static org.assertj.core.api.Assertions.*;
import javaslang.collection.Seq;
import javaslang.control.Option;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
/**
* Integration tests for {@link PersonRepository} showing Javaslang support at repsoitory query methods.
*
* @author Oliver Gierke
*/
@RunWith(SpringRunner.class)
@Transactional
@SpringBootTest
public class PersonRepositoryIntegrationTests {
@Autowired PersonRepository people;
@SpringBootApplication
static class Configuration {}
/**
* @see #231
*/
@Test
public void readsPersonIntoJavaslangOptionById() {
Person dave = people.save(new Person("Dave", "Matthews"));
Option<Person> result = people.findById(dave.getId());
assertThat(result.isEmpty()).isFalse();
assertThat(result.get()).isEqualTo(dave);
}
/**
* @see #231
*/
@Test
public void readsPeopleIntoJavaslangSeq() {
Person dave = people.save(new Person("Dave", "Matthews"));
Person carter = people.save(new Person("Carter", "Beauford"));
Seq<Person> result = people.findByFirstnameContaining("art");
assertThat(result.contains(carter)).isTrue();
assertThat(result.contains(dave)).isFalse();
}
}

View File

@@ -21,6 +21,7 @@
<module>showcase</module>
<module>interceptors</module>
<module>java8</module>
<module>javaslang</module>
<module>jpa21</module>
<module>security</module>
<module>multiple-datasources</module>