Created
August 28, 2018 10:32
-
-
Save michael-simons/6a95921c293faa8d3da68caa37c8598e to your computer and use it in GitHub Desktop.
Use Spring Data Neo4js injectable OGM Session
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>transactionandcustomsession</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<packaging>jar</packaging> | |
<name>transactionandcustomsession</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.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-test</artifactId> | |
<scope>test</scope> | |
</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> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-maven-plugin</artifactId> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
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.transactionandcustomsession; | |
import java.util.Map; | |
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.neo4j.annotation.Query; | |
import org.springframework.data.neo4j.repository.Neo4jRepository; | |
import org.springframework.stereotype.Component; | |
import org.springframework.stereotype.Repository; | |
import org.springframework.transaction.annotation.Transactional; | |
interface SearchRepositoryCustom { | |
Iterable<ThingEntity> searchByCriteria(); | |
} | |
@NodeEntity | |
class ThingEntity { | |
private Long id; | |
private String name; | |
ThingEntity() { | |
} | |
public ThingEntity(Integer i) { | |
this.name = "Thing " + i; | |
} | |
@Override public String toString() { | |
return "ThingEntity{" + | |
"name='" + name + '\'' + | |
'}'; | |
} | |
} | |
interface ThingRepository extends Neo4jRepository<ThingEntity, Long> { | |
@Query("MATCH (t:ThingEntity) RETURN t LIMIT 10") | |
public Iterable<ThingEntity> searchByCriteria(); | |
} | |
@Repository | |
@Transactional | |
class SearchRepositoryImpl implements SearchRepositoryCustom { | |
private final Session session; | |
public SearchRepositoryImpl(Session session) { | |
this.session = session; | |
} | |
@Override | |
public Iterable<ThingEntity> searchByCriteria() { | |
String query = "MATCH (t:ThingEntity) RETURN t LIMIT 10"; | |
return session.query(ThingEntity.class, query, Map.of()); | |
} | |
} | |
@SpringBootApplication | |
public class TransactionandcustomsessionApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(TransactionandcustomsessionApplication.class, args); | |
} | |
} | |
@Component | |
class ExampleUsage implements CommandLineRunner { | |
private final ThingRepository thingRepository; | |
private final SearchRepositoryCustom searchRepositoryCustom; | |
public ExampleUsage(ThingRepository thingRepository, SearchRepositoryCustom searchRepositoryCustom) { | |
this.thingRepository = thingRepository; | |
this.searchRepositoryCustom = searchRepositoryCustom; | |
} | |
@Override | |
public void run(String... args) { | |
this.thingRepository.save(new ThingEntity(1)); | |
this.thingRepository.save(new ThingEntity(2)); | |
var things = this.searchRepositoryCustom.searchByCriteria(); | |
things.forEach(System.out::println); | |
things = this.thingRepository.searchByCriteria(); | |
things.forEach(System.out::println); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment