Created
August 28, 2018 09:59
-
-
Save michael-simons/8b37f4e07936e69c6dd33dbed4d7d780 to your computer and use it in GitHub Desktop.
Enforce a concrete implementation of Pageable for paged Queries with Spring Data (Neo4j)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package soquestions.custompagerequest; | |
import static java.util.stream.Collectors.*; | |
import java.io.Serializable; | |
import java.util.stream.IntStream; | |
import org.neo4j.ogm.annotation.NodeEntity; | |
import org.neo4j.ogm.session.Session; | |
import org.springframework.boot.CommandLineRunner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.PageRequest; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.data.domain.Sort; | |
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; | |
import org.springframework.data.neo4j.repository.support.SimpleNeo4jRepository; | |
import org.springframework.data.repository.CrudRepository; | |
import org.springframework.data.repository.NoRepositoryBean; | |
import org.springframework.stereotype.Component; | |
class AppPageRequest extends PageRequest implements Pageable { | |
private AppPageRequest(int page, int size, Sort sort) { | |
super(page - 1, size, sort); | |
} | |
public static AppPageRequest of(int page, int size) { | |
return of(page, size, Sort.unsorted()); | |
} | |
public static AppPageRequest of(int page, int size, Sort sort) { | |
return new AppPageRequest(page, size, sort); | |
} | |
} | |
@NoRepositoryBean | |
interface BaseRepository<T, ID extends Serializable> extends CrudRepository<T, ID> { | |
Page<T> findAll(AppPageRequest appPageRequest); | |
} | |
@NoRepositoryBean | |
class BaseRepositoryImpl<T, ID extends Serializable> extends SimpleNeo4jRepository<T, ID> | |
implements BaseRepository<T, ID> { | |
public BaseRepositoryImpl(Class<T> domainClass, Session session) { | |
super(domainClass, session); | |
} | |
public Page<T> findAll(AppPageRequest appPageRequest) { | |
return super.findAll(appPageRequest); | |
} | |
} | |
@NodeEntity | |
class ThingEntity { | |
private Long id; | |
private String name; | |
ThingEntity() { | |
} | |
public ThingEntity(Integer i) { | |
this.name = "Thing " + i; | |
} | |
public String getName() { | |
return name; | |
} | |
} | |
interface ThingRepository extends BaseRepository<ThingEntity, Long> { | |
} | |
@SpringBootApplication | |
@EnableNeo4jRepositories(repositoryBaseClass = BaseRepositoryImpl.class) | |
public class CustomPagerequestApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(CustomPagerequestApplication.class, args); | |
} | |
} | |
@Component | |
class ExampleUsage implements CommandLineRunner { | |
private final ThingRepository thingRepository; | |
public ExampleUsage(ThingRepository thingRepository) { | |
this.thingRepository = thingRepository; | |
} | |
@Override | |
public void run(String... args) { | |
var things = IntStream.iterate(1, i -> i <= 10, i -> i + 1) | |
.mapToObj(ThingEntity::new) | |
.collect(toList()); | |
this.thingRepository.saveAll(things); | |
var page = this.thingRepository.findAll(AppPageRequest.of(1, 5)); | |
page.stream().map(ThingEntity::getName).forEach(System.out::println); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<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> | |
<groupId>soquestions</groupId> | |
<artifactId>custom-pagerequest</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>custom-pagerequest</name> | |
<description>Demo project for Spring Boot</description> | |
<parent> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-parent</artifactId> | |
<version>2.0.4.RELEASE</version> | |
<relativePath/> <!-- lookup parent from repository --> | |
</parent> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | |
<java.version>10</java.version> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-neo4j</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.neo4j</groupId> | |
<artifactId>neo4j-ogm-embedded-driver</artifactId> | |
<version>${neo4j-ogm.version}</version> | |
</dependency> | |
<dependency> | |
<groupId>org.neo4j</groupId> | |
<artifactId>neo4j</artifactId> | |
<version>3.4.5</version> | |
<scope>runtime</scope> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment