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

NullPointerException at doing Session.load() while first time trying out Neo4J OGM

我是第一次尝试 Neo4J OGM,通过其 github page and with the help of its 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    }

正在扫描的包裹 (movies.domain) 似乎不包含您的实体 类。所以实体没有保存,id为null。

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

还有一个小示例应用程序:https://github.com/neo4j-examples/neo4j-ogm-university/tree/2.0