Skip to content

Provides support to increase developer productivity in Java when using the neo4j graph database. Uses familiar Spring concepts such as a template classes for core API usage and provides an annotation based programming model using AspectJ

License

nmervaillie/spring-data-neo4j

 
 

Repository files navigation

Spring Data Neo4j

The primary goal of the Spring Data project is to make it easier to build Spring-powered applications that use new data access technologies such as non-relational databases, map-reduce frameworks, and cloud based data services.

The Spring Data Neo4j project aims to provide a familiar and consistent Spring-based programming model for integrating with the Neo4j Graph Database.

Features

  • Automatic mapping of annotated domain entities for nodes and relationships;

  • Powerful CRUD based repositories with provided, derived, and annotated finder methods;

  • Integration with Spring’s comprehensive Transaction management;

  • Agnostic communication with Neo4j through a variety of transport mechanisms (embedded, http, bolt);

  • Integration into Spring Data REST;

  • Seamless integration with Spring Boot.

Documentation & Getting Help

This README is the best place to start learning about the features of Spring Data Neo4j.

To learn more refer to:

If you are new to Spring as well as to Spring Data, look for information about Spring projects.

Quick start

Add the Maven dependency:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.2.0.M1</version>
    </dependency>

    <repository>
      <id>spring-maven-milestone</id>
      <name>Springframework Maven Milestone Repository</name>
      <url>http://repo.spring.io/libs-milestone</url>
    </repository>

If you’d rather like the latest snapshots of the upcoming major version, use our Maven snapshot repository and declare the appropriate dependency version.

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.2.0.BUILD-SNAPSHOT</version>
    </dependency>

    <!-- used for nightly builds -->
    <repository>
      <id>spring-maven-snapshot</id>
      <snapshots><enabled>true</enabled></snapshots>
      <name>Springframework Maven SNAPSHOT Repository</name>
      <url>http://repo.spring.io/libs-release</url>
    </repository>
Tip
You can check out how to get setup with Gradle in the the Reference Manual.

Configure Spring Data Neo4j in your application using JavaConfig bean configuration

@Configuration
@ComponentScan(basePackages = "org.example.person.services",...)
@EnableNeo4jRepositories(basePackages = "com.example.person.repository",...)
@EnableTransactionManagement
public class MyConfiguration {

    @Bean
    public SessionFactory sessionFactory() {
        // with domain entity base package(s)
        return new SessionFactory("com.example.person.domain",...);
    }

	@Bean
	public Neo4jTransactionManager transactionManager() {
		return new Neo4jTransactionManager(sessionFactory());
	}
}

Spring Data Neo4j provides support for connecting to all of Neo4j’s java drivers:

  • Bolt

  • HTTP

  • Embedded

Spring Data Neo4j will attempt to auto-configure itself using a file called ogm.properties, which it expects to find on root of the classpath. Here we will configure the HTTP Driver.

driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://user:password@localhost:7474

The application can be configured programmatically as well, please consult the reference guide for more information.

Annotate an entity:

@NodeEntity
public class Person {
    private Long id;
    private String name;

    @Relationship(type = "FRIEND", direction = "OUTGOING")
    private Set<Person> friends;

    public Person() {}
    public Person(String name) { this.name = name; }

    private void knows(Person friend) { friends.add(friend); }
}

To simplify the creation of data repositories Spring Data Neo4j provides a generic repository programming model. It will automatically create a repository proxy for you that adds implementations of finder methods you specify on an interface.

For example, given the Person class above, a PersonRepository interface that can query for Person by name and when the name matches a like expression is shown below:

@Repository
public interface PersonRepository extends Neo4jRepository<Person> {

  List<Person> findByName(String name);

  List<Person> findByNameLike(String name);
}

The queries issued on execution will be derived from the method name.

Typically you will want to call your domain objects and repositories from services. In this Service we find the repository interface and register a proxy object in the container:

@Service
public class MyService {

    @Autowired
    private final PersonRepository repository;

    @Transactional
    public void doWork() {

        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
        Person thatSamejon = personRepository.findOne(id, 2);
        for (Person friend : jon.getFriends()) {
            System.out.println("Jon's friends to depth 2: " + friend);
        }
    }
}

Contributing to Spring Data Neo4j

There are dedicated, mandatory 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 Neo4j Google Group and by helping on StackOverflow.

  • Create 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 contribution guidelines for details.

About

Provides support to increase developer productivity in Java when using the neo4j graph database. Uses familiar Spring concepts such as a template classes for core API usage and provides an annotation based programming model using AspectJ

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%