简体   繁体   中英

Can't add more relationship to one node using neo4j spring data

I have 2 node type with N:N relationship

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?

this is my code:

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

@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

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

Is this neo4j spring data issue? I use neo4j 3.0.1 and spring-data-neo4j 4.1.1.RELEASE version.

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? 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.

Update :

Also make sure the student is added to SchoolClass so that your entities are consistent before saving

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM