Preface
What I did this week was a bug in the alice project. After the reclassification, there were no students in the original class, and there was no analysis of the performance of a certain course in the original class.
For example, I used to be in Computer Class 1806 (there are 9 classes in Computer Major, namely Computer 1801-Computer 1809). Because the major majors are divided into minor majors, I am divided into Software 181. If the class and students are a pair There are no more students in Computer Class 1806 at this time, but the score analysis is an analysis of the scores of a certain course arrangement of a class. It is necessary to obtain the scores of all the students in this class. As a result, the score analysis cannot be related to all students. achievement.
solve
At first, I thought it would be troublesome to change the relationship between entities. What we want is an array of grades for everyone in the class. Since there are no students in the class, everyone's grades are not found. But everyone's scores are still stored in the database and not lost, so we just need to look up in another way. I added the corresponding score array to the score_summary entity, so that the demand can be met without changing the many-to-one. When I was about to finish the change, the senior said that he still wanted to see all the students in the original class in the class management, so he had to change the many-to-many relationship between the students and the class.
At this time, it is necessary to change the one-to-many relationship between the class and the students into a many-to-many relationship. So that the original computer class 1806 can also find all the students in the class before the placement. At the same time, it is necessary to set a one-to-many relationship between the class and the student array to indicate the current class of the student.
/**
* 学生当前所在班级
*/
@ManyToOne
private Klass klass;
/**
* 学生历史所在班级数组(包含当前所在班级)
*/
@ManyToMany(mappedBy = "studentList")
@JsonView(KlassListJsonView.class)
private List<Klass> klassList = new ArrayList<>();
Then come to revise the problems arising from the current changes. First of all, because students change from many-to-one to many-to-many for the class, and the class is responsible for maintenance, and the klass_student_list intermediate table is automatically established in the database. But we find that all students in the class have no data, we need to restore all the students' data in the original class first. The students also keep the current class information, and we use these data to put the students in the same class into the student array of the class. In this way, the intermediate table has relevant data.
List<Klass> klassList = this.klassRepository.findAll();
List<Student> allStudent = this.studentRepository.findAll();
for (Klass klass: klassList) {
List<Student> studentList = new ArrayList<>();
for (Student student: allStudent) {
if (student.getKlass() != null && klass.getId().equals(student.getKlass().getId())) {
studentList.add(student);
}
}
klass.setStudentList(studentList);
}
this.klassRepository.saveAll(klassList);
Then we will modify the code when the students change the class. In order to separate the business and technology thinking, we use the aop form to modify, which also reduces the changes to the original code. The idea is to delete students in the old class and add students in the new class when students change classes.
@Before(value = "annotationPointCut(id, student)", argNames = "id, student")
public void before(Long id, Student student) {
Student newStudent = this.studentService.findById(id);
Klass oldKlass = newStudent.getKlass();
Klass newKlass = this.klassService.findById(student.getKlass().getId());
// 如果新班级与旧班级不相等
if (!oldKlass.getId().equals(newKlass.getId())) {
// 删除旧班级中的这个学生
int i =0;
for (Student student1: oldKlass.getStudentList()) {
if (student1.getId().equals(student.getId())) {
oldKlass.getStudentList().remove(i);
break;
} else {
i++;
}
}
// 在新班级中添加这个学生
newKlass.getStudentList().add(newStudent);
// 保存
this.klassService.save(oldKlass);
this.klassService.save(newKlass);
}
}
But I don’t know how to operate the situation of reclassification. I still need to communicate with the senior. I also need to use aop to modify this situation. Just set and add all students in the new class.
Possible problems
I encountered a problem when initializing all student data in the class. After the update, there was no data in the intermediate table. Then I interrupted to look at the problem and found that when querying the class in many-to-many mode, all students in the class will not be found out. , This is hibernate's lazy query mechanism.
The solution is to turn off lazy queries on @ManyToMany
@ManyToMany(fetch = FetchType.EAGER)
private List<Student> studentList = new ArrayList<>();
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。