绩效问题的循环关系

时间:2019-03-26 11:25:09

标签: java neo4j cypher neo4j-ogm

使用neo4j OGM保持循环关系(3个对象)时,我遇到性能问题:https://i.imgur.com/OP43FTB.png

规则:

  • 1门课程有1门学科和1位老师
  • 老师有多个课程和多个主题
  • 主题有多位老师和多门课程

根据保存的实例数,保存时间不是线性的,在一定数量的对象之后,操作“从不”完成。

使用以下版本:neo4j-ogm-core和neo4j-ogm-bolt驱动程序版本3.1.8

基础实体类:

@NodeEntity
public abstract class Entity {

    @Id
    @GeneratedValue
    private Long id;

    @Index(unique = true)
    private String uuid;

    public Entity() {
        uuid = UUID.randomUUID().toString();
    }

    public Long getId() {
        return id;
    }

    public String getUuid() {
        return uuid;
    }

    @Override
    public int hashCode() {
        return Objects.hash(uuid);
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Entity)) {
            return false;
        }

        Entity other = (Entity) obj;

        return Objects.equals(getUuid(), other.getUuid());
    }
}

@NodeEntity
public class Course extends Entity {

    private String name;

    @Relationship(type = "SUBJECT_TAUGHT")
    private Subject subject;

    @Relationship(type = "TEACHES_CLASS", direction = Relationship.INCOMING)
    private Teacher teacher;

    public String getName() {
        return name;
    }

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

    public Subject getSubject() {
        return subject;
    }

    public void setSubject(Subject subject) {
        this.subject = subject;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

}

@NodeEntity
public class Subject extends Entity {

    private String name;

    @Relationship(type = "TAUGHT_BY")
    private Set<Teacher> teachers = new HashSet<>();

    @Relationship(type = "SUBJECT_TAUGHT", direction = "INCOMING")
    private Set<Course> courses = new HashSet<>();

    public String getName() {
        return name;
    }

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

    public Set<Teacher> getTeachers() {
        return teachers;
    }

    public void setTeachers(Set<Teacher> teachers) {
        this.teachers = teachers;
    }

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

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

}

@NodeEntity
public class Teacher extends Entity {

    private String name;

    @Relationship(type = "TEACHES_CLASS")
    private Set<Course> courses = new HashSet<>();

    @Relationship(type = "TAUGHT_BY", direction = Relationship.INCOMING)
    private Set<Subject> subjects = new HashSet<>();

    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;
    }

    public Set<Subject> getSubjects() {
        return subjects;
    }

    public void setSubjects(Set<Subject> subjects) {
        this.subjects = subjects;
    }

}

以这种方式生成并保留课程列表:

    public T createOrUpdate(T entity) {
        session.save(entity);
        return find(entity.getId());
    }

将每个班级生成的10个实例(10个课程,10个教师,10个主题)链接在一起时,保存方法永远不会结束。

谢谢。

0 个答案:

没有答案