java—从spring获取数据时,一些数据会多次显示

cu6pst1q  于 2021-07-15  发布在  Java
关注(0)|答案(1)|浏览(339)

我在java spring中有一个用于学习的数据库:

如果员工没有借来的书,所有的工作。
如果员工有2本书(或更多),并且该员工注册了任何课程,那么我在同一课程中看到该员工2次(如果他有3本书,那么3次,4次,等等)。

如果我使用查询“select*from registration”就可以了

github上的项目-https://github.com/katzzer/sqldemoapplicationnewversion
显示数据库数据的控制器

  1. @GetMapping("/employees/courseDetail")
  2. public String getCourses(@RequestParam("courseId") long id, Model model) {
  3. Optional<Course> courseOptional = courseService.getCourse(id);
  4. if (courseOptional.isEmpty()) {
  5. return "redirect:/employees/courses/";
  6. }
  7. Course course = courseOptional.get();
  8. model.addAttribute("course", course);
  9. List<Employee> employees = employeeService.getAllEmployees();
  10. model.addAttribute("employees", employees);
  11. List<Employee> enrolledEmployees = course.getEmployees();
  12. model.addAttribute("enrolledEmployees", enrolledEmployees);
  13. return "CourseDetailPage";
  14. }

员工类别:

  1. @Entity(name = "Employee")
  2. @Table(name = "employee")
  3. @Transactional
  4. public class Employee {
  5. @Id
  6. @SequenceGenerator(
  7. name = "employee_sequence",
  8. sequenceName = "employee_sequence",
  9. allocationSize = 1
  10. )
  11. @GeneratedValue(
  12. strategy = GenerationType.SEQUENCE,
  13. generator = "employee_sequence"
  14. )
  15. @Column(name ="id")
  16. private Long id;
  17. @Column(
  18. name = "name",
  19. nullable = false,
  20. columnDefinition = "TEXT"
  21. )
  22. private String name;
  23. @Column(
  24. name = "city",
  25. nullable = false,
  26. columnDefinition = "TEXT"
  27. )
  28. private String city;
  29. @Column(
  30. name = "language",
  31. nullable = false,
  32. columnDefinition = "TEXT"
  33. )
  34. private String language;
  35. @OneToOne(
  36. mappedBy = "employee",
  37. orphanRemoval = true,
  38. cascade = {CascadeType.PERSIST, CascadeType.REMOVE}
  39. )
  40. private EmployeeIdCard employeeIdCard;
  41. @OneToMany(
  42. mappedBy = "employee",
  43. orphanRemoval = true,
  44. cascade = {CascadeType.PERSIST, CascadeType.REMOVE},
  45. fetch = FetchType.EAGER
  46. )
  47. private List<Book> books = new ArrayList<>();
  48. @ManyToMany(
  49. cascade = {CascadeType.PERSIST, CascadeType.REMOVE},
  50. fetch = FetchType.EAGER
  51. )
  52. @Fetch(value = FetchMode.SUBSELECT)
  53. @JoinTable(
  54. name = "enrolment",
  55. joinColumns = @JoinColumn(
  56. name = "employee_id",
  57. foreignKey = @ForeignKey(name = "enrolment_employee_id_fk")
  58. ),
  59. inverseJoinColumns = @JoinColumn(
  60. name = "course_id",
  61. foreignKey = @ForeignKey(name = "enrolment_course_id_fk")
  62. )
  63. )
  64. private List<Course> courses = new ArrayList<>();
  65. public Employee(String name, String city, String language) {
  66. this.name = name;
  67. this.city = city;
  68. this.language = language;
  69. }
  70. public Employee(String name, String city, String language, EmployeeIdCard employeeIdCard) {
  71. this.name = name;
  72. this.city = city;
  73. this.language = language;
  74. this.employeeIdCard = employeeIdCard;
  75. }
  76. public Employee() {
  77. }
  78. public void addBook(Book book) {
  79. if (!this.books.contains(book)) {
  80. this.books.add(book);
  81. book.setEmployee(this);
  82. }
  83. }
  84. public void removeBook(Book book) {
  85. if (this.books.contains(book)) {
  86. this.books.remove(book);
  87. book.setEmployee(null);
  88. }
  89. }
  90. public void setEmployeeIdCard(EmployeeIdCard employeeIdCard) {
  91. this.employeeIdCard = employeeIdCard;
  92. }
  93. public List<Book> getBooks() {
  94. return books;
  95. }
  96. public Long getId() {
  97. return id;
  98. }
  99. public void setId(Long id) {
  100. this.id = id;
  101. }
  102. public String getName() {
  103. return name;
  104. }
  105. public void setName(String name) {
  106. this.name = name;
  107. }
  108. public String getCity() {
  109. return city;
  110. }
  111. public void setCity(String city) {
  112. this.city = city;
  113. }
  114. public String getLanguage() {
  115. return language;
  116. }
  117. public void setLanguage(String language) {
  118. this.language = language;
  119. }
  120. public EmployeeIdCard getEmployeeIdCard() {
  121. return employeeIdCard;
  122. }
  123. public void setBooks(List<Book> books) {
  124. this.books = books;
  125. }
  126. public List<Course> getCourses() {
  127. return courses;
  128. }
  129. public void enrolToCourse(Course course) {
  130. courses.add(course);
  131. course.getEmployees().add(this);
  132. }
  133. public void unEnrolToCourse(Course course) {
  134. courses.remove(course);
  135. course.getEmployees().remove(this);

图书类

  1. @Entity(name = "Book")
  2. @Table(name = "book")
  3. public class Book {
  4. @Id
  5. @SequenceGenerator(
  6. name = "book_sequence",
  7. sequenceName = "book_sequence",
  8. allocationSize = 1
  9. )
  10. @GeneratedValue(
  11. strategy = SEQUENCE,
  12. generator = "book_sequence"
  13. )
  14. @Column(
  15. name = "id",
  16. updatable = false
  17. )
  18. private Long id;
  19. @Column(
  20. name = "created_at",
  21. nullable = false,
  22. columnDefinition = "TIMESTAMP WITHOUT TIME ZONE"
  23. )
  24. private LocalDateTime createdAt;
  25. @Column(
  26. name = "book_name",
  27. nullable = false
  28. )
  29. private String bookName;
  30. @ManyToOne (fetch = FetchType.EAGER)
  31. @JoinColumn(
  32. name = "employee_id",
  33. // nullable = false,
  34. referencedColumnName = "id",
  35. foreignKey = @ForeignKey (
  36. name = "employee_book_fk"
  37. )
  38. )
  39. private Employee employee;
  40. public Book() {
  41. }
  42. public Book(String bookName, LocalDateTime createdAt) {
  43. this.bookName = bookName;
  44. this.createdAt = createdAt;
  45. }
  46. public Long getId() {
  47. return id;
  48. }
  49. public void setId(Long id) {
  50. this.id = id;
  51. }
  52. public LocalDateTime getCreatedAt() {
  53. return createdAt;
  54. }
  55. public void setCreatedAt(LocalDateTime createdAt) {
  56. this.createdAt = createdAt;
  57. }
  58. public String getBookName() {
  59. return bookName;
  60. }
  61. public void setBookName(String bookName) {
  62. this.bookName = bookName;
  63. }
  64. public Employee getEmployee() {
  65. return employee;
  66. }
  67. public void setEmployee(Employee employee) {
  68. this.employee = employee;
  69. }

课程类别:

  1. @Entity(name = "Course")
  2. @Table(name = "course")
  3. public class Course {
  4. @Id
  5. @SequenceGenerator(
  6. name = "course_sequence",
  7. sequenceName = "course_sequence",
  8. allocationSize = 1
  9. )
  10. @GeneratedValue(
  11. strategy = SEQUENCE,
  12. generator = "course_sequence"
  13. )
  14. @Column(
  15. name = "id",
  16. updatable = false
  17. )
  18. private Long id;
  19. @Column(
  20. name = "course_name",
  21. nullable = false,
  22. columnDefinition = "TEXT"
  23. )
  24. private String courseName;
  25. @Column(
  26. name = "department",
  27. nullable = false,
  28. columnDefinition = "TEXT"
  29. )
  30. private String department;
  31. @ManyToMany(
  32. mappedBy = "courses",
  33. fetch = FetchType.EAGER
  34. )
  35. private List<Employee> employees = new ArrayList<>();
  36. public Course(String courseName, String department) {
  37. this.courseName = courseName;
  38. this.department = department;
  39. }
  40. public Course() {
  41. }
  42. public Long getId() {
  43. return id;
  44. }
  45. public void setId(Long id) {
  46. this.id = id;
  47. }
  48. public String getCourseName() {
  49. return courseName;
  50. }
  51. public void setCourseName(String courseName) {
  52. this.courseName = courseName;
  53. }
  54. public String getDepartment() {
  55. return department;
  56. }
  57. public void setDepartment(String department) {
  58. this.department = department;
  59. }
  60. public List<Employee> getEmployees() {
  61. return employees;
  62. }
  63. public void setEmployees(List<Employee> employees) {
  64. this.employees = employees;
  65. }

课程服务:

  1. @Service
  2. public class CourseService {
  3. private CourseRepository courseRepository;
  4. @Autowired
  5. public CourseService(CourseRepository courseRepository) {
  6. this.courseRepository = courseRepository;
  7. }
  8. public void saveCourse(Course course) {
  9. courseRepository.save(course);
  10. }
  11. public void saveAllCourses(List<Course> courses) {
  12. courseRepository.saveAll(courses);
  13. }
  14. public Optional<Course> getCourse (Long id) {
  15. return courseRepository.findById(id);
  16. }
  17. public List<Course> getAllCourses() {
  18. return courseRepository.findAll();
  19. }
  20. public void deleteCourse(Long id) {
  21. courseRepository.deleteById(id);
  22. }
  23. }

课程假设:

  1. public interface CourseRepository extends JpaRepository<Course, Long> {
  2. }
gt0wga4j

gt0wga4j1#

我想:

  1. private Set<Employee> employees = new HashSet<>();

也许能解决你的问题。而且在 @ManyToMany 加入尽量不要使用 List<...> 原因列表允许重复,而集合不允许重复元素。

相关问题