Skip to content

Commit 2c5f92e

Browse files
authored
Merge pull request eugenp#5607 from fanatixan/bael-2190
bael-2190
2 parents 06bb965 + d2a4341 commit 2c5f92e

9 files changed

Lines changed: 512 additions & 0 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.manytomany.model;
2+
3+
import java.util.Set;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.ManyToMany;
9+
import javax.persistence.OneToMany;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "course")
14+
public class Course {
15+
16+
@Id
17+
@Column(name = "id")
18+
private Long id;
19+
20+
@ManyToMany(mappedBy = "likedCourses")
21+
private Set<Student> likes;
22+
23+
@OneToMany(mappedBy = "course")
24+
private Set<CourseRating> ratings;
25+
26+
@OneToMany(mappedBy = "course")
27+
private Set<CourseRegistration> registrations;
28+
29+
// additional properties
30+
31+
public Course() {
32+
}
33+
34+
public Long getId() {
35+
return id;
36+
}
37+
38+
public Set<CourseRating> getRatings() {
39+
return ratings;
40+
}
41+
42+
public Set<CourseRegistration> getRegistrations() {
43+
return registrations;
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
final int prime = 31;
49+
int result = 1;
50+
result = prime * result + ((id == null) ? 0 : id.hashCode());
51+
return result;
52+
}
53+
54+
@Override
55+
public boolean equals(Object obj) {
56+
if (this == obj)
57+
return true;
58+
if (obj == null)
59+
return false;
60+
if (getClass() != obj.getClass())
61+
return false;
62+
Course other = (Course) obj;
63+
if (id == null) {
64+
if (other.id != null)
65+
return false;
66+
} else if (!id.equals(other.id))
67+
return false;
68+
return true;
69+
}
70+
71+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.baeldung.manytomany.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.EmbeddedId;
5+
import javax.persistence.Entity;
6+
import javax.persistence.JoinColumn;
7+
import javax.persistence.ManyToOne;
8+
import javax.persistence.MapsId;
9+
import javax.persistence.Table;
10+
11+
@Entity
12+
@Table(name = "course_rating")
13+
public class CourseRating {
14+
15+
@EmbeddedId
16+
private CourseRatingKey id;
17+
18+
@ManyToOne
19+
@MapsId("student_id")
20+
@JoinColumn(name = "student_id")
21+
private Student student;
22+
23+
@ManyToOne
24+
@MapsId("course_id")
25+
@JoinColumn(name = "course_id")
26+
private Course course;
27+
28+
@Column(name = "rating")
29+
private int rating;
30+
31+
public CourseRating() {
32+
}
33+
34+
public int getRating() {
35+
return rating;
36+
}
37+
38+
public void setRating(int rating) {
39+
this.rating = rating;
40+
}
41+
42+
public CourseRatingKey getId() {
43+
return id;
44+
}
45+
46+
public Student getStudent() {
47+
return student;
48+
}
49+
50+
public Course getCourse() {
51+
return course;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
final int prime = 31;
57+
int result = 1;
58+
result = prime * result + ((id == null) ? 0 : id.hashCode());
59+
return result;
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (this == obj)
65+
return true;
66+
if (obj == null)
67+
return false;
68+
if (getClass() != obj.getClass())
69+
return false;
70+
CourseRating other = (CourseRating) obj;
71+
if (id == null) {
72+
if (other.id != null)
73+
return false;
74+
} else if (!id.equals(other.id))
75+
return false;
76+
return true;
77+
}
78+
79+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.manytomany.model;
2+
3+
import java.io.Serializable;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Embeddable;
7+
8+
@Embeddable
9+
public class CourseRatingKey implements Serializable {
10+
11+
@Column(name = "student_id")
12+
private Long studentId;
13+
14+
@Column(name = "course_id")
15+
private Long courseId;
16+
17+
public CourseRatingKey() {
18+
}
19+
20+
public Long getStudentId() {
21+
return studentId;
22+
}
23+
24+
public Long getCourseId() {
25+
return courseId;
26+
}
27+
28+
@Override
29+
public int hashCode() {
30+
final int prime = 31;
31+
int result = 1;
32+
result = prime * result + ((courseId == null) ? 0 : courseId.hashCode());
33+
result = prime * result + ((studentId == null) ? 0 : studentId.hashCode());
34+
return result;
35+
}
36+
37+
@Override
38+
public boolean equals(Object obj) {
39+
if (this == obj)
40+
return true;
41+
if (obj == null)
42+
return false;
43+
if (getClass() != obj.getClass())
44+
return false;
45+
CourseRatingKey other = (CourseRatingKey) obj;
46+
if (courseId == null) {
47+
if (other.courseId != null)
48+
return false;
49+
} else if (!courseId.equals(other.courseId))
50+
return false;
51+
if (studentId == null) {
52+
if (other.studentId != null)
53+
return false;
54+
} else if (!studentId.equals(other.studentId))
55+
return false;
56+
return true;
57+
}
58+
59+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package com.baeldung.manytomany.model;
2+
3+
import java.time.LocalDateTime;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.ManyToOne;
10+
import javax.persistence.Table;
11+
12+
@Entity
13+
@Table(name = "course_registration")
14+
public class CourseRegistration {
15+
16+
@Id
17+
@Column(name = "id")
18+
private Long id;
19+
20+
@ManyToOne
21+
@JoinColumn(name = "student_id")
22+
private Student student;
23+
24+
@ManyToOne
25+
@JoinColumn(name = "course_id")
26+
private Course course;
27+
28+
@Column(name = "registered_at")
29+
private LocalDateTime registeredAt;
30+
31+
@Column(name = "grade")
32+
private int grade;
33+
34+
// additional properties
35+
36+
public CourseRegistration() {
37+
}
38+
39+
public Student getStudent() {
40+
return student;
41+
}
42+
43+
public void setStudent(Student student) {
44+
this.student = student;
45+
}
46+
47+
public Course getCourse() {
48+
return course;
49+
}
50+
51+
public void setCourse(Course course) {
52+
this.course = course;
53+
}
54+
55+
public LocalDateTime getRegisteredAt() {
56+
return registeredAt;
57+
}
58+
59+
public void setRegisteredAt(LocalDateTime registeredAt) {
60+
this.registeredAt = registeredAt;
61+
}
62+
63+
public int getGrade() {
64+
return grade;
65+
}
66+
67+
public void setGrade(int grade) {
68+
this.grade = grade;
69+
}
70+
71+
public Long getId() {
72+
return id;
73+
}
74+
75+
@Override
76+
public int hashCode() {
77+
final int prime = 31;
78+
int result = 1;
79+
result = prime * result + ((id == null) ? 0 : id.hashCode());
80+
return result;
81+
}
82+
83+
@Override
84+
public boolean equals(Object obj) {
85+
if (this == obj)
86+
return true;
87+
if (obj == null)
88+
return false;
89+
if (getClass() != obj.getClass())
90+
return false;
91+
CourseRegistration other = (CourseRegistration) obj;
92+
if (id == null) {
93+
if (other.id != null)
94+
return false;
95+
} else if (!id.equals(other.id))
96+
return false;
97+
return true;
98+
}
99+
100+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.manytomany.model;
2+
3+
import java.util.Set;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.Id;
8+
import javax.persistence.JoinColumn;
9+
import javax.persistence.JoinTable;
10+
import javax.persistence.ManyToMany;
11+
import javax.persistence.OneToMany;
12+
import javax.persistence.Table;
13+
14+
@Entity
15+
@Table(name = "student")
16+
public class Student {
17+
18+
@Id
19+
@Column(name = "id")
20+
private Long id;
21+
22+
@ManyToMany
23+
@JoinTable(name = "course_like", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
24+
private Set<Course> likedCourses;
25+
26+
@OneToMany(mappedBy = "student")
27+
private Set<CourseRating> ratings;
28+
29+
@OneToMany(mappedBy = "student")
30+
private Set<CourseRegistration> registrations;
31+
32+
// additional properties
33+
34+
public Student() {
35+
}
36+
37+
public Long getId() {
38+
return id;
39+
}
40+
41+
public Set<Course> getLikedCourses() {
42+
return likedCourses;
43+
}
44+
45+
public Set<CourseRating> getRatings() {
46+
return ratings;
47+
}
48+
49+
public Set<CourseRegistration> getRegistrations() {
50+
return registrations;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
final int prime = 31;
56+
int result = 1;
57+
result = prime * result + ((id == null) ? 0 : id.hashCode());
58+
return result;
59+
}
60+
61+
@Override
62+
public boolean equals(Object obj) {
63+
if (this == obj)
64+
return true;
65+
if (obj == null)
66+
return false;
67+
if (getClass() != obj.getClass())
68+
return false;
69+
Student other = (Student) obj;
70+
if (id == null) {
71+
if (other.id != null)
72+
return false;
73+
} else if (!id.equals(other.id))
74+
return false;
75+
return true;
76+
}
77+
78+
}

0 commit comments

Comments
 (0)