Git Product home page Git Product logo

mangrish / spring-data-neo4j Goto Github PK

View Code? Open in Web Editor NEW

This project forked from spring-projects/spring-data-neo4j

0.0 1.0 0.0 18.74 MB

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

Home Page: http://www.springsource.org/spring-data/neo4j

License: Apache License 2.0

Java 100.00%

spring-data-neo4j's Introduction

Note
This branch (master) now points to Spring Data Neo4j 4.x the new implementation that is based on Neo4j-OGM.
Caution
If you are using Spring Data Neo4j 4.0 please update the underlying dependeny on Neo4j-OGM to org.neo4j:neo4j-ogm:1.1.5 to get the latest and greatest performance updates and bug-fixes. Using the latest Neo4j-OGM version is also recommended in general.

Spring Data Neo4j 4.x - Quick start

@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 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 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 (embedded, http, [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
<dependencies>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-neo4j</artifactId>
        <version>4.1.0.BUILD-SNAPSHOT</version> <!-- or .M1 when released -->
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>spring-maven-snapshot</id>
        <snapshots><enabled>true</enabled></snapshots>
        <name>Springframework Maven MILESTONE Repository</name>
        <url>http://maven.springframework.org/milestone</url>
    </repository>
</repositories>

Spring configuration

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

MyConfiguration.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 and Embedded 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
driver=org.neo4j.ogm.drivers.http.driver.HttpDriver
URI=http://user:password@localhost:7474

The application can be configured programmatically as well, please 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 :

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 reference manual

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 SDN University sample project for more information.

More example projects for Spring Data Neo4j 4 are available in the Neo4j-Examples repository

Getting Help

This README and the Reference Manual are the best places to start learning about Spring Data Neo4j 4.

The main 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 StackOverflow

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.

spring-data-neo4j's People

Contributors

atg103 avatar bachmanm avatar jexp avatar luanne avatar mp911de avatar odrotbohm avatar spring-builds avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.