218 lines
7.1 KiB
Plaintext
218 lines
7.1 KiB
Plaintext
= Spring Data Neo4j 4.1.x - Quick start
|
|
|
|
[source,java]
|
|
----
|
|
@NodeEntity
|
|
class Person {
|
|
private Long id;
|
|
private String name;
|
|
|
|
@Relationship(type = "FRIEND", direction = "UNDIRECTED")
|
|
private Set<Person> friends;
|
|
|
|
public Person() {}
|
|
public Person(String name) { this.name = name; }
|
|
|
|
private void knows(Person friend) { friends.add(friend); }
|
|
}
|
|
|
|
public interface PersonRepository extends GraphRepository<Person> {
|
|
}
|
|
|
|
Person jon = new Person("Jon");
|
|
Person emil = new Person("Emil");
|
|
Person rod = new Person("Rod");
|
|
|
|
emil.knows(jon);
|
|
emil.knows(rod);
|
|
|
|
// Persist entities and relationships to graph database
|
|
personRepository.save(emil);
|
|
|
|
for (Person friend : emil.getFriends()) {
|
|
System.out.println("Friend: " + friend);
|
|
}
|
|
|
|
// Control loading depth
|
|
jon = personRepository.findOne(id, 2);
|
|
for (Person friend : jon.getFriends()) {
|
|
System.out.println("Jon's friends to depth 2: " + friend);
|
|
}
|
|
----
|
|
|
|
== About
|
|
|
|
The primary goal of the http://projects.spring.io/spring-data[Spring Data] project is to make it more convenient and consistent to build Spring-based applications that use modern data technologies.
|
|
Spring Data Neo4j integrates the leading http://neo4j.com/[Neo4j] Graph Database.
|
|
|
|
The Spring Data Neo4j project provides a simplified POJO based programming model that reduces that amount of boilerplate code needed to create Neo4j applications.
|
|
|
|
It supports:
|
|
|
|
* automatic mapping annotated domain entities for nodes and relationships
|
|
* interface based repositories with provided, derived, and annotated finder methods
|
|
* transaction control
|
|
* multi-transport (HTTP, Embedded, Bolt)
|
|
* exception translation
|
|
* integration into Spring Data REST
|
|
* works well within Spring Boot
|
|
|
|
== License
|
|
|
|
Spring Data Neo4j is licensed under the Apache License v 2.0.
|
|
The underlying technology, neo4j-ogm-core, -compiler, -http-driver, -bolt-driver and the neo4j-java-driver are Apache licensed too.
|
|
|
|
== Using Spring Data Neo4j
|
|
|
|
=== Maven configuration
|
|
|
|
* Add the maven repository and dependency:
|
|
|
|
.pom.xml
|
|
[source,xml]
|
|
----
|
|
<dependencies>
|
|
<dependency>
|
|
<groupId>org.springframework.data</groupId>
|
|
<artifactId>spring-data-neo4j</artifactId>
|
|
<version>4.1.6</version>
|
|
</dependency>
|
|
</dependencies>
|
|
----
|
|
|
|
|
|
=== Spring configuration
|
|
|
|
* Configure Spring Data Neo4j 4.1 in your application using Java-based bean configuration
|
|
|
|
.MyConfiguration.java
|
|
[source,java]
|
|
----
|
|
@Configuration
|
|
@EnableNeo4jRepositories(basePackages = "com.example.person.repository",...)
|
|
@EnableTransactionManagement
|
|
public class MyConfiguration extends Neo4jConfiguration {
|
|
|
|
@Bean
|
|
public SessionFactory getSessionFactory() {
|
|
// with domain entity base package(s)
|
|
return new SessionFactory("com.example.person.domain",...);
|
|
}
|
|
|
|
// needed for session in view in web-applications
|
|
@Bean
|
|
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
|
|
public Session getSession() throws Exception {
|
|
return super.getSession();
|
|
}
|
|
|
|
}
|
|
----
|
|
|
|
Spring Data Neo4j 4.1 provides support for connecting to Neo4j using different drivers.
|
|
HTTP, Embedded and Bolt drivers are available.
|
|
Spring Data Neo4j will attempt to auto-configure itself using a file called `ogm.properties`, which it expects to find on the classpath.
|
|
|
|
.ogm.properties
|
|
[source,java]
|
|
----
|
|
driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
|
|
URI=http://user:password@localhost:7474
|
|
----
|
|
|
|
The application can be configured programmatically as well, please http://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#_spring_configuration[read the reference guide] for more information.
|
|
|
|
=== Graph entities
|
|
|
|
* Annotate your entity class. In this case it is a 'Person' class that has a relationship to the 'Company' they work at :
|
|
|
|
[source,java]
|
|
----
|
|
package com.example.person.domain;
|
|
|
|
@NodeEntity
|
|
class Person {
|
|
private Long id;
|
|
private String name;
|
|
|
|
@Relationship(type = "WORKS_AT", direction = "OUTGOING")
|
|
private Company employer;
|
|
|
|
public Person() {}
|
|
public Person(String name) { this.name = name; }
|
|
|
|
private void worksAt(Company employer) { this.employer = employer; }
|
|
}
|
|
----
|
|
|
|
=== Transactional services
|
|
|
|
Create a repository or service to perform typical operations on your entities.
|
|
The complete functionality is covered in the http://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/#reference_programming-model[reference manual]
|
|
|
|
[source,java]
|
|
----
|
|
package com.example.person.repository;
|
|
|
|
public interface PersonRepository extends GraphRepository<Person> {
|
|
|
|
// derived finder method
|
|
Person findByName(String name);
|
|
|
|
@Query("MATCH (c:Company)<-[:WORKS_AT]-(p:Person) WHERE id(c) = {company} RETURN p")
|
|
List<Person> findEmployees(Company company);
|
|
}
|
|
|
|
package com.example.person.service;
|
|
|
|
@Service
|
|
@Transactional
|
|
public class EmployeeService {
|
|
|
|
@Autowired
|
|
private PersonRepository personRepository;
|
|
|
|
public int getNumberOfPeople() {
|
|
return personRepository.count();
|
|
}
|
|
|
|
public Person createPerson(String name) {
|
|
return personRepository.save(new Person(name));
|
|
}
|
|
|
|
public List<Person> getAllPeople() {
|
|
return personRepository.findAll();
|
|
}
|
|
|
|
public List<Person> getEmployees(Company c) {
|
|
return personRepository.findEmployees(c);
|
|
}
|
|
}
|
|
----
|
|
|
|
Please see the https://github.com/neo4j-examples/sdn4-university/tree/4.1[SDN University sample project] for more information.
|
|
|
|
More example projects for Spring Data Neo4j 4 are available in the https://github.com/neo4j-examples?query=sdn4[Neo4j-Examples] repository
|
|
|
|
== Getting Help
|
|
|
|
This README and the http://static.springsource.org/spring-data/data-neo4j/docs/current/reference/html/[Reference Manual] are the best places to start learning about Spring Data Neo4j 4.
|
|
|
|
The main http://projects.spring.io/spring-data-neo4j[SpringSource project site] contains links to basic project information such as source code, JavaDocs, Issue tracking, etc.
|
|
|
|
For more detailed questions, use the "forum":http://forum.springsource.org/forumdisplay.php?f=80. If you are new to Spring as well as to Spring Data, look for information about "Spring projects":http://www.springsource.org/projects.
|
|
|
|
|
|
You will also find help on http://stackoverflow.com/questions/tagged/spring-data-neo4j[StackOverflow]
|
|
|
|
== Contributing to Spring Data Neo4j
|
|
|
|
There are dedicated, mandatory https://github.com/spring-projects/spring-data-build/blob/master/CONTRIBUTING.adoc[contribution guidelines] for all Spring Data projects.
|
|
|
|
Here are some ways for you to get involved in the community:
|
|
|
|
* Get involved with Spring Data Neo4j community on the http://groups.google.com/group/neo4j[Neo4j Google Group] and by helping on http://stackoverflow.com/questions/tagged/spring-data-neo4j[StackOverflow].
|
|
* Create https://jira.springframework.org/browse/DATAGRAPH[JIRA] tickets for bugs and new features and comment and vote on the ones that you are interested in.
|
|
* Github is for social coding: if you want to write code, we encourage contributions through *pull requests* from a fork of this repository.
|
|
If you want to contribute code this way, please read the https://github.com/spring-projects/spring-data-build/blob/master/CONTRIBUTING.adoc[contribution guidelines] for details.
|