第一次尝试使用Neo4J OGM时,在执行Session.load()时出现NullPointerException

时间:2016-08-02 14:50:17

标签: neo4j neo4j-ogm

我是第一次尝试使用Neo4J OGM来自github page并在其manual的帮助下提供的简单示例。

我正在运行neo4j-community-3.0.0-M05

我遇到以下异常:

Exception in thread "main" java.lang.NullPointerException
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.lookup(LoadOneDelegate.java:56)
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:49)
    at org.neo4j.ogm.session.delegates.LoadOneDelegate.load(LoadOneDelegate.java:39)
    at org.neo4j.ogm.session.Neo4jSession.load(Neo4jSession.java:137)
    at org.neo4j.ogm.session.Capability$LoadOne$load.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:110)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:130)
    at Main.main(Main.groovy:24)

例外情况发生在Main.groovy的第24行。我调试了。它发生在session.load()电话上。

我觉得这必须是因为我必须在设置依赖项时犯了一些错误。但是无法弄明白。

这是我的代码:

的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mahesha999.exp</groupId>
  <artifactId>Neo4JTemp</artifactId>
  <version>0.0.1-SNAPSHOT</version>      
    <dependencies>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-core</artifactId>
            <version>2.0.4</version>
        </dependency>   
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-http-driver</artifactId>
            <version>2.0.4</version>
        </dependency>   
    </dependencies>    
</project>

ogm.properties

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

Actor.groovy

import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.annotation.GraphId

@NodeEntity
public class Actor {    
    @GraphId
    private Long id;
    private String name;

    @Relationship(type = "ACTS_IN", direction = "OUTGOING")
    private Set<Movie> movies = new HashSet<>();

    public Actor() { }

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

    public void actsIn(Movie movie) {
        movies<< movie;
        movie.getActors() << this;
    }
}

Movie.groovy

import org.neo4j.ogm.annotation.NodeEntity
import org.neo4j.ogm.annotation.Relationship
import org.neo4j.ogm.annotation.GraphId

@NodeEntity
public class Movie {    
    @GraphId
    private Long id;
    private String title;
    private int released;

    @Relationship(type = "ACTS_IN", direction = "INCOMING")
    List<Actor> actors = [];

    public Movie() {}

    public Movie(String title, int year) {
        this.title = title;
        this.released = year;
    }    
}

Main.groovy

1    import org.neo4j.ogm.session.Session
2    import org.neo4j.ogm.session.SessionFactory
3    
4    class Main {
5       
6       static main(def args)
7       {
8           //Set up the Session
9           SessionFactory sessionFactory = new SessionFactory("movies.domain");
10          Session session = sessionFactory.openSession();
11          
12          Movie movie = new Movie("The Matrix", 1999);
13          
14          Actor keanu = new Actor("Keanu Reeves");
15          keanu.actsIn(movie);
16          
17          Actor carrie = new Actor("Carrie-Ann Moss");
18          carrie.actsIn(movie);
19      
20          //Persist the movie. This persists the actors as well.
21          session.save(movie);
22                  
23          //Load a movie
24          Movie matrix = session.load(Movie.class, movie.id);
25          for(Actor actor : matrix.getActors()) {
26              System.out.println("Actor: " + actor.name);
27          }
28      }   
29    }

1 个答案:

答案 0 :(得分:2)

正在扫描的软件包(movies.domain)似乎不包含您的实体类。这就是为什么实体没有保存,id为空。

OGM参考指南(附教程)在这里:http://neo4j.com/docs/ogm-manual/current/

一个小样本应用:https://github.com/neo4j-examples/neo4j-ogm-university/tree/2.0