我在java spring中有一个用于学习的数据库:
如果员工没有借来的书,所有的工作。
如果员工有2本书(或更多),并且该员工注册了任何课程,那么我在同一课程中看到该员工2次(如果他有3本书,那么3次,4次,等等)。
如果我使用查询“select*from registration”就可以了
github上的项目-https://github.com/katzzer/sqldemoapplicationnewversion
显示数据库数据的控制器
@GetMapping("/employees/courseDetail")
public String getCourses(@RequestParam("courseId") long id, Model model) {
Optional<Course> courseOptional = courseService.getCourse(id);
if (courseOptional.isEmpty()) {
return "redirect:/employees/courses/";
}
Course course = courseOptional.get();
model.addAttribute("course", course);
List<Employee> employees = employeeService.getAllEmployees();
model.addAttribute("employees", employees);
List<Employee> enrolledEmployees = course.getEmployees();
model.addAttribute("enrolledEmployees", enrolledEmployees);
return "CourseDetailPage";
}
员工类别:
@Entity(name = "Employee")
@Table(name = "employee")
@Transactional
public class Employee {
@Id
@SequenceGenerator(
name = "employee_sequence",
sequenceName = "employee_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "employee_sequence"
)
@Column(name ="id")
private Long id;
@Column(
name = "name",
nullable = false,
columnDefinition = "TEXT"
)
private String name;
@Column(
name = "city",
nullable = false,
columnDefinition = "TEXT"
)
private String city;
@Column(
name = "language",
nullable = false,
columnDefinition = "TEXT"
)
private String language;
@OneToOne(
mappedBy = "employee",
orphanRemoval = true,
cascade = {CascadeType.PERSIST, CascadeType.REMOVE}
)
private EmployeeIdCard employeeIdCard;
@OneToMany(
mappedBy = "employee",
orphanRemoval = true,
cascade = {CascadeType.PERSIST, CascadeType.REMOVE},
fetch = FetchType.EAGER
)
private List<Book> books = new ArrayList<>();
@ManyToMany(
cascade = {CascadeType.PERSIST, CascadeType.REMOVE},
fetch = FetchType.EAGER
)
@Fetch(value = FetchMode.SUBSELECT)
@JoinTable(
name = "enrolment",
joinColumns = @JoinColumn(
name = "employee_id",
foreignKey = @ForeignKey(name = "enrolment_employee_id_fk")
),
inverseJoinColumns = @JoinColumn(
name = "course_id",
foreignKey = @ForeignKey(name = "enrolment_course_id_fk")
)
)
private List<Course> courses = new ArrayList<>();
public Employee(String name, String city, String language) {
this.name = name;
this.city = city;
this.language = language;
}
public Employee(String name, String city, String language, EmployeeIdCard employeeIdCard) {
this.name = name;
this.city = city;
this.language = language;
this.employeeIdCard = employeeIdCard;
}
public Employee() {
}
public void addBook(Book book) {
if (!this.books.contains(book)) {
this.books.add(book);
book.setEmployee(this);
}
}
public void removeBook(Book book) {
if (this.books.contains(book)) {
this.books.remove(book);
book.setEmployee(null);
}
}
public void setEmployeeIdCard(EmployeeIdCard employeeIdCard) {
this.employeeIdCard = employeeIdCard;
}
public List<Book> getBooks() {
return books;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public EmployeeIdCard getEmployeeIdCard() {
return employeeIdCard;
}
public void setBooks(List<Book> books) {
this.books = books;
}
public List<Course> getCourses() {
return courses;
}
public void enrolToCourse(Course course) {
courses.add(course);
course.getEmployees().add(this);
}
public void unEnrolToCourse(Course course) {
courses.remove(course);
course.getEmployees().remove(this);
图书类
@Entity(name = "Book")
@Table(name = "book")
public class Book {
@Id
@SequenceGenerator(
name = "book_sequence",
sequenceName = "book_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = SEQUENCE,
generator = "book_sequence"
)
@Column(
name = "id",
updatable = false
)
private Long id;
@Column(
name = "created_at",
nullable = false,
columnDefinition = "TIMESTAMP WITHOUT TIME ZONE"
)
private LocalDateTime createdAt;
@Column(
name = "book_name",
nullable = false
)
private String bookName;
@ManyToOne (fetch = FetchType.EAGER)
@JoinColumn(
name = "employee_id",
// nullable = false,
referencedColumnName = "id",
foreignKey = @ForeignKey (
name = "employee_book_fk"
)
)
private Employee employee;
public Book() {
}
public Book(String bookName, LocalDateTime createdAt) {
this.bookName = bookName;
this.createdAt = createdAt;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
课程类别:
@Entity(name = "Course")
@Table(name = "course")
public class Course {
@Id
@SequenceGenerator(
name = "course_sequence",
sequenceName = "course_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = SEQUENCE,
generator = "course_sequence"
)
@Column(
name = "id",
updatable = false
)
private Long id;
@Column(
name = "course_name",
nullable = false,
columnDefinition = "TEXT"
)
private String courseName;
@Column(
name = "department",
nullable = false,
columnDefinition = "TEXT"
)
private String department;
@ManyToMany(
mappedBy = "courses",
fetch = FetchType.EAGER
)
private List<Employee> employees = new ArrayList<>();
public Course(String courseName, String department) {
this.courseName = courseName;
this.department = department;
}
public Course() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCourseName() {
return courseName;
}
public void setCourseName(String courseName) {
this.courseName = courseName;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
课程服务:
@Service
public class CourseService {
private CourseRepository courseRepository;
@Autowired
public CourseService(CourseRepository courseRepository) {
this.courseRepository = courseRepository;
}
public void saveCourse(Course course) {
courseRepository.save(course);
}
public void saveAllCourses(List<Course> courses) {
courseRepository.saveAll(courses);
}
public Optional<Course> getCourse (Long id) {
return courseRepository.findById(id);
}
public List<Course> getAllCourses() {
return courseRepository.findAll();
}
public void deleteCourse(Long id) {
courseRepository.deleteById(id);
}
}
课程假设:
public interface CourseRepository extends JpaRepository<Course, Long> {
}
1条答案
按热度按时间gt0wga4j1#
我想:
也许能解决你的问题。而且在
@ManyToMany
加入尽量不要使用List<...>
原因列表允许重复,而集合不允许重复元素。