gpt4 book ai didi

java - 修复向 ManyToMany 添加实体的问题

转载 作者:行者123 更新时间:2023-12-01 21:57:05 25 4
gpt4 key购买 nike

我需要检查用户的电子邮件是否已添加到学生列表中,如果是,请将此用户与类(class)连接。

java.lang.UnsupportedOperationException
at java.util.Collections$1.remove(Collections.java:4684)
at java.util.AbstractCollection.clear(AbstractCollection.java:436)
at org.hibernate.collection.internal.PersistentSet.clear(PersistentSet.java:318)
at org.hibernate.type.CollectionType.replaceElements(CollectionType.java:581)
at org.hibernate.type.CollectionType.replace(CollectionType.java:757)
at org.hibernate.type.TypeHelper.replace(TypeHelper.java:167)

用户实体

@ManyToMany(cascade = CascadeType.MERGE)
@JoinTable(
name = "Student_Courses",
joinColumns = {@JoinColumn(name = "student_id")},
inverseJoinColumns = {@JoinColumn(name = "course_id")}
)
private Set<Course> availableCourses = new HashSet<>();

类(class)实体

@ManyToMany(mappedBy = "availableCourses")
private Set<User> users = new HashSet<>();

用户服务

public void bindStudentWithCoursesAfterRegistration(String email) {
User user = userRepo.findFirstByEmail(email);
List<CourseStudentEmails> studentEntriesInCourses = courseStudentEmailsRepo.findAllByEmail(email);

if (studentEntriesInCourses.size() != 0){
for (CourseStudentEmails entry : studentEntriesInCourses) {
Course course = entry.getCourse();
user.getAvailableCourses().add(course);
}
}
userRepo.save(user);//Exception throws here
}

注册 Controller

    @PostMapping("/registration")
public String addUser(User user, @RequestParam(value = "checkboxTeacher", required = false) String checkboxValue, Model model) {
[//chek if user is already registered]
userRepo.save(user);
courseService.bindStudentWithCoursesAfterRegistration(user);
return "redirect:/login";

}

最佳答案

我猜您缺少正确的查询来查找适当的CourseStudentEmails

如果您在实体中保留字符串集合作为电子邮件 (CourseStudentEmails),您可以根据特定电子邮件查询实体,如下所示:

@Query("SELECT c FROM CourseStudentEmails c WHERE ?1 member of c.emails")
List<CourseStudentEmails> findAllByEmail(String email);

关于java - 修复向 ManyToMany 添加实体的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58736282/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com