Coder Social home page Coder Social logo

Comments (4)

sparmboy avatar sparmboy commented on August 14, 2024

Upon further investigation, it looks as though this only occurs when enabling Spring Data Neo4j repositories. I have created a simple repo to reproduce this but unfortunately im unable to push the repo to github as im behind a corporate firewall. Appologies but I will post each file here:

App.java

package com.mypackage;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan("com.mypackage")
@EnableAutoConfiguration
public class App
{
    public static void main(String args[])
    {
    SpringApplication.run(App.class, args);     
    } 

}

CustomNeo4jConfiguration.java

package com.mypackage;

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.rest.graphdb.RestGraphDatabase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.neo4j.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.core.GraphDatabase;
import org.springframework.data.neo4j.support.DelegatingGraphDatabase;
import org.springframework.data.neo4j.support.Neo4jTemplate;

@Configuration
//@EnableNeo4jRepositories("com.mypackage")
public class CustomNeo4jConfiguration
{
    GraphDatabaseService localDatabaseService;

    /*@Bean
    @DependsOn("graphDatabase")
    public Neo4jTemplate neo4jTemplate()
    {
        return new Neo4jTemplate(graphDatabase());
    }*/

    @Bean
    @Autowired
    @DependsOn("graphDatabaseService")
    public GraphDatabase graphDatabase()
    {
        if (localDatabaseService instanceof GraphDatabase) return (GraphDatabase) localDatabaseService;
        return new DelegatingGraphDatabase(localDatabaseService);
    }


    @Bean
    public GraphDatabaseService graphDatabaseService()
    {
        if (this.localDatabaseService == null)
        {
            String remoteDBURL = "http://localhost:7474/db/data";
            System.setProperty("org.neo4j.rest.read_timeout","30");
            System.setProperty("org.neo4j.rest.connect_timeout","30");
            System.setProperty("org.neo4j.rest.driver","neo4j-rest-graphdb/2.0.1");
            System.setProperty("org.neo4j.rest.stream","true");
            System.setProperty("org.neo4j.rest.batch_transaction","false");
            System.setProperty("org.neo4j.rest.logging_filter","false");

            this.localDatabaseService = new RestGraphDatabase(remoteDBURL);

        }

        return this.localDatabaseService;
    }

}

MyNodeEntity.java

package com.mypackage;

import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;


@NodeEntity
public class MyNodeEntity
{
    @GraphId
    Long id;
}

MyNodeRepository.java

package com.mypackage;

import org.springframework.data.neo4j.repository.GraphRepository;


public interface MyNodeRepository extends GraphRepository<MyNodeEntity>
{

}

MyService.java

package com.mypackage;

import org.neo4j.graphdb.GraphDatabaseService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService implements InitializingBean      
{
    @Autowired
    GraphDatabaseService gds;

    public void afterPropertiesSet() throws Exception
    {
        System.out.println("Success = " + gds.getAllNodes().iterator().next() );
    }


}

pom.xml

<?xml version="1.0"?>
<project
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
    xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>0.5.0.M6</version>
        <!-- <version>1.0.0.RC1</version> -->
    </parent>

    <groupId>com.mypackage</groupId>
    <artifactId>neo4j-remote</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>neo4j-remote</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>3.0.0.RC1</version>
        </dependency>


        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-rest-graphdb</artifactId>
            <version>2.0.0</version>
        </dependency>


    </dependencies>




    <repositories>
        <repository>
            <id>neo4j-release-repository</id>
            <name>Neo4j Maven 2 release repository</name>
            <url>http://m2.neo4j.org/content/repositories/releases/</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>neo4j-snapshot-repository</id>
            <name>Neo4j Maven 2 snapshot repository</name>
            <url>http://m2.neo4j.org/snapshots</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
        <repository>
            <id>Sun</id>
            <url>http://download.java.net/maven/2</url>
        </repository>

    </repositories>


</project>

To run this:

  • Startup a local instance of Neo4j server on localhost:7474 (ensure you have a least one node in it)

  • Build the app by running

    mvn clean package

  • Run the app with:

    java -jar target\neo4j-remote-1.0-SNAPSHOT.jar

  • You should see the app connect and dump out the line:

    Success = http://localhost:7474/db/data/node/0

  • Now in the CustomNeo4jConfiguration uncomment the following two sections:

    @EnableNeo4jRepositories("com.mypackage")

and
@Bean @DependsOn("graphDatabase") public Neo4jTemplate neo4jTemplate() { return new Neo4jTemplate(graphDatabase()); }

  • Now re-build and run again and you should see the NullPointerException thrown

from java-rest-binding.

AndrewWestberg avatar AndrewWestberg commented on August 14, 2024

Is there a work-around for this issue? Or is using a RestGraphDatabase and spring-boot just plain broken?

from java-rest-binding.

jexp avatar jexp commented on August 14, 2024

Andrew it is not broken at all. Just wrapping a RestGraphDatabase in a DelegatingDatabase is nothing anyone would ever do. Just use SpringRestGraphDatabase(url)

See this example: https://github.com/neo4j-contrib/developer-resources/tree/gh-pages/language-guides/java/spring-data-neo4j

from java-rest-binding.

AndrewWestberg avatar AndrewWestberg commented on August 14, 2024

@jexp Thanks much. Very helpful.

from java-rest-binding.

Related Issues (20)

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.