无法通过Maven从Java向Neo4j数据库插入多个记录

时间:2018-07-29 09:16:01

标签: java neo4j cypher graph-databases neo4j-ogm

我正在尝试从此网站实施代码 https://www.javahelps.com/2015/10/object-graph-mapping-with-neo4j-ogm.html?m=1

当我运行主文件时,我只能插入1条记录,即爱丽丝及其关系。我无法插入其他记录,而是收到一条错误消息,指出

error from intellij

这是我的Student.java文件

package com.neo4jexample.ogmtest.entities;


import java.util.HashSet;
import java.util.Set;

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

@NodeEntity
public class Student {


@GraphId
private Long id;

private String name;

@Relationship(type = "ENROLLED", direction = Relationship.OUTGOING)
private Set<Course> courses = new HashSet<Course>();

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Set<Course> getCourses() {
    return courses;
}

public void setCourses(Set<Course> courses) {
    this.courses = courses;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

这是我的Course.java文件

package com.neo4jexample.ogmtest.entities;

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

@NodeEntity

public class Course {

@GraphId
private Long id;

private String name;

private float credits;

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public float getCredits() {
    return credits;
}

public void setCredits(float credits) {
    this.credits = credits;
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Course other = (Course) obj;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

}

这是我的Main.java文件

package com.neo4jexample.ogmtest;

import java.util.Collections;

import com.neo4jexample.ogmtest.entities.Course;
import com.neo4jexample.ogmtest.entities.Student;
import org.neo4j.ogm.session.Session;
import org.neo4j.ogm.session.SessionFactory;


public class Main {

public static final String NEO4J_URL = "http://localhost:7474";
public static final String USERNAME = "neo4j";
public static final String PASSWORD = "pp4281";

public static void main(String[] args) {



    // Create SessionFactory. Pass the package name of the entity classes as
    // the argument.
    SessionFactory sessionFactory = new SessionFactory("com.neo4jexample.ogmtest.entities");

    // Create the session
    Session session = sessionFactory.openSession(NEO4J_URL, USERNAME, PASSWORD);

    // Create few courses
    Course oop = new Course();
    oop.setName("Object Oriented Programming");
    oop.setCredits(2.0f);

    Course algo = new Course();
    algo.setName("Advanced Algorithm");
    algo.setCredits(3.0f);

    Course db = new Course();
    db.setName("Database Internals");
    db.setCredits(3.0f);

    // Create few students
    Student alice = new Student();
    alice.setName("Alice");

    Student bob = new Student();
    bob.setName("Bob");

    Student carol = new Student();
    carol.setName("Carol");

    // Add the courses
    alice.getCourses().add(oop);
    alice.getCourses().add(algo);
    alice.getCourses().add(db);

    bob.getCourses().add(oop);
    bob.getCourses().add(algo);

    carol.getCourses().add(algo);
    carol.getCourses().add(db);

    // Persist the objects. Persisting students persists courses as        well.
    session.save(alice);
    session.save(bob);
    session.save(carol);

    // Retrieve Students who enrolled for Advanced Algorithm
    Iterable<Student> students = session.query(Student.class,
            "MATCH (c:Course)<-[:ENROLLED]-(student) WHERE c.name = 'Advanced Algorithm' RETURN student",
            Collections.<String, Object> emptyMap());

    // Print all the Students
    for (Student stu : students) {
        System.out.println(stu.getName());
    }
}

}

这是我的Pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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.neo4jexample</groupId>
<artifactId>neo4jsampleconnection</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-ogm</artifactId>
        <version>1.1.3</version>
    </dependency>
</dependencies>

</project>

1 个答案:

答案 0 :(得分:0)

看起来我正在使用Neo4j版本3,所以我降级到2.3版本,它可以正常工作!