简体   繁体   English

无法使用neo4j spring数据向一个节点添加更多关系

[英]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.

相关问题 删除与Spring Data Neo4j的关系 - Delete relationship with Spring Data Neo4j 在 Spring Data Neo4j 中查询关系 - Querying a Relationship in Spring Data Neo4j Spring Data Neo4j 4.0.0:无法在具有相同标签的节点之间创建关系 - Spring Data Neo4j 4.0.0: Can't Create Relationship Between Nodes with the Same Label 如何使用 spring 数据 neo4j 将给定的 label 添加到给定节点? - How to add a given label to given node by using spring data neo4j? Spring Data Neo4j节点NotfoundException - Spring Data Neo4j Node NotfoundException Neo4j Spring数据:是否可以将关系实体用作其他关系实体的末端节点? - Neo4j Spring Data : Is it possible to use Relationship Entity as End Node of other Relationship Entity? 春季数据neo4j 4无法投射日期 - Spring data neo4j 4 Date can't cast 如何在spring数据neo4j中正确编码与相同类型节点的层次关系? - How to code the hierarchical relationship to the node of the same type properly in spring data neo4j? 如何使用 Java API 在 Neo4j 中的节点/关系上添加用户定义的约束 - How to add user defined constraints on a node/relationship in Neo4j using Java API Spring Data Neo4j中的DynamicRelationshipType或在运行时定义关系类型 - DynamicRelationshipType in Spring Data Neo4j or defining relationship types at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM