Skip to content

Commit 9c434f2

Browse files
committed
bael-2190
1 parent ab821cb commit 9c434f2

5 files changed

Lines changed: 369 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)