[英]Can't add more relationship to one node using neo4j spring data
I have 2 node type with N:N relationship 我有2个节点类型,N:N关系
For Example Student -> Class 例如学生 - >班级
one Class can have more than one student, and one Student can study on more than one Class 一个班级可以有一个以上的学生,一个学生可以学习一个以上的班级
where I import students to one class from excel file, I have problem that I don't know why? 我从excel文件导入学生到一个班级,我有问题,我不知道为什么?
this is my code: 这是我的代码:
Student.java Student.java
@NodeEntity
public class User implements Serializable {
@GraphId
Long id;
String userCode;
String email;
String password;
String realName;
Date birthday;
String phone;
Integer gender;
Integer status;
Integer roleType;
@Relationship(type=RelationshipType.CLASS_OF_STUDENT, direction=Relationship.OUTGOING)
List<SchoolClass> listStudentClass;
}
SchoolClass.java SchoolClass.java
@NodeEntity
public class SchoolClass implements Serializable {
@GraphId
Long id;
String className;
String classCode;
String createUser;
Date createDate;
String updateUser;
Date updateDate;
@Relationship(type=RelationshipType.CLASS_OF_STUDENT, direction=Relationship.INCOMING)
List<User> students;
}
where I loop to import student from excel as this 我循环从excel导入学生的地方
for(...) {
...
List<SchoolClass> listStudentClass = new ArrayList<SchoolClass>();
listStudentClass.add(schoolClass);
student.setListStudentClass(listStudentClass);
...
userRepository.save(student);
}
but only the last one student have relationship with schoolClass when finish loop 但是当完成循环时,只有最后一个学生与schoolClass有关系
Is this neo4j spring data issue? 这是neo4j spring数据问题吗? I use neo4j 3.0.1 and spring-data-neo4j 4.1.1.RELEASE version.
我使用neo4j 3.0.1和spring-data-neo4j 4.1.1.RELEASE版本。
It looks like you're always creating a new List of classes and adding a single class to it per row from your Excel sheet? 您似乎总是创建一个新的类列表,并从Excel工作表中为每行添加一个类? This will result in adding the single new relation and removing all old ones (which is probably why you have only the last student relationship saved).
这将导致添加单个新关系并删除所有旧关系(这可能是为什么您只保存了最后一个学生关系)。
Instead, add the class to the existing list listStudentClass
on the User entity which you would have either created or loaded in the same session. 而是将类添加到User实体上的现有列表
listStudentClass
,您将在同一会话中创建或加载该实体。
Update : 更新 :
Also make sure the student is added to SchoolClass
so that your entities are consistent before saving 还要确保将学生添加到
SchoolClass
以便在保存之前实体一致
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.