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

Notifications You must be signed in to change notification settings

aleksa2285/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.RELEASE</version>
    </dependency>

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.1.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>

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

Configure Spring Data Neo4j in your application using JavaConfig bean configuration

@Configuration
@ComponentScan(basePackages = "org.example.person.services",...)
@EnableExperimentalNeo4jRepositories(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.

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 = "UNDIRECTED")
    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:

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:

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

Quick start (SDN Version 4.1.x)

Add the Maven dependency:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.1.2.RELEASE</version>
    </dependency>

Configure Spring Data Neo4j in your application using Java-based bean configuration

@Configuration
@ComponentScan(basePackages = "org.example.person.services",...)
@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 will attempt to auto-configure itself using a file called ogm.properties, which it expects to find on root of the classpath.

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

Annotate an entity: In this case a 'Person' class has a relationship to the 'Company' they work at :

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; }
}

Create a repository or service to perform typical operations on your entities.

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;

Wire up the repository into a 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);
    }
}

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%