diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java new file mode 100644 index 0000000000..4849165c57 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java @@ -0,0 +1,67 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; + +@Entity +public class Course { + + @Id + private Long id; + + @ManyToMany(mappedBy = "likedCourses") + private Set likes; + + @OneToMany(mappedBy = "course") + private Set ratings; + + @OneToMany(mappedBy = "course") + private Set registrations; + + // additional properties + + public Course() { + } + + public Long getId() { + return id; + } + + public Set getRatings() { + return ratings; + } + + public Set getRegistrations() { + return registrations; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Course other = (Course) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java new file mode 100644 index 0000000000..1b6c9d8b2c --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java @@ -0,0 +1,75 @@ +package com.baeldung.manytomany.extracolumn.model; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; + +@Entity +public class CourseRating { + + @EmbeddedId + private CourseRatingKey id; + + @ManyToOne + @MapsId("student_id") + @JoinColumn(name = "student_id") + private Student student; + + @ManyToOne + @MapsId("course_id") + @JoinColumn(name = "course_id") + private Course course; + + private int rating; + + public CourseRating() { + } + + public int getRating() { + return rating; + } + + public void setRating(int rating) { + this.rating = rating; + } + + public CourseRatingKey getId() { + return id; + } + + public Student getStudent() { + return student; + } + + public Course getCourse() { + return course; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CourseRating other = (CourseRating) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java new file mode 100644 index 0000000000..6638ae6968 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java @@ -0,0 +1,59 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class CourseRatingKey implements Serializable { + + @Column(name = "student_id") + private Long studentId; + + @Column(name = "course_id") + private Long courseId; + + public CourseRatingKey() { + } + + public Long getStudentId() { + return studentId; + } + + public Long getCourseId() { + return courseId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((courseId == null) ? 0 : courseId.hashCode()); + result = prime * result + ((studentId == null) ? 0 : studentId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CourseRatingKey other = (CourseRatingKey) obj; + if (courseId == null) { + if (other.courseId != null) + return false; + } else if (!courseId.equals(other.courseId)) + return false; + if (studentId == null) { + if (other.studentId != null) + return false; + } else if (!studentId.equals(other.studentId)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java new file mode 100644 index 0000000000..225968dba4 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java @@ -0,0 +1,94 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.time.LocalDateTime; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class CourseRegistration { + + @Id + private Long id; + + @ManyToOne + @JoinColumn(name = "student_id") + private Student student; + + @ManyToOne + @JoinColumn(name = "course_id") + private Course course; + + private LocalDateTime registeredAt; + + private int grade; + + // additional properties + + public CourseRegistration() { + } + + public Student getStudent() { + return student; + } + + public void setStudent(Student student) { + this.student = student; + } + + public Course getCourse() { + return course; + } + + public void setCourse(Course course) { + this.course = course; + } + + public LocalDateTime getRegisteredAt() { + return registeredAt; + } + + public void setRegisteredAt(LocalDateTime registeredAt) { + this.registeredAt = registeredAt; + } + + public int getGrade() { + return grade; + } + + public void setGrade(int grade) { + this.grade = grade; + } + + public Long getId() { + return id; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CourseRegistration other = (CourseRegistration) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java new file mode 100644 index 0000000000..6bc8271e7e --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java @@ -0,0 +1,74 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; + +@Entity +public class Student { + + @Id + private Long id; + + @ManyToMany + @JoinTable(name = "course_like", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id")) + private Set likedCourses; + + @OneToMany(mappedBy = "student") + private Set ratings; + + @OneToMany(mappedBy = "student") + private Set registrations; + + // additional properties + + public Student() { + } + + public Long getId() { + return id; + } + + public Set getLikedCourses() { + return likedCourses; + } + + public Set getRatings() { + return ratings; + } + + public Set getRegistrations() { + return registrations; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Student other = (Student) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +}