diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Course.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Course.java index bdfb8e890f..d824505629 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Course.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Course.java @@ -1,14 +1,9 @@ package com.baeldung.manytomany.model; +import javax.persistence.*; +import java.util.HashSet; import java.util.Set; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.ManyToMany; -import javax.persistence.OneToMany; -import javax.persistence.Table; - @Entity @Table(name = "course") public class Course { @@ -18,31 +13,55 @@ public class Course { private Long id; @ManyToMany(mappedBy = "likedCourses") - private Set likes; + private Set likes = new HashSet<>(); @OneToMany(mappedBy = "course") - private Set ratings; + private Set ratings = new HashSet<>(); @OneToMany(mappedBy = "course") - private Set registrations; + private Set registrations = new HashSet<>(); // additional properties public Course() { } + public Course(Long id) { + this.id = id; + } + public Long getId() { return id; } + public void setId(Long id) { + this.id = id; + } + + public Set getLikes() { + return likes; + } + + public void setLikes(Set likes) { + this.likes = likes; + } + public Set getRatings() { return ratings; } + public void setRatings(Set ratings) { + this.ratings = ratings; + } + public Set getRegistrations() { return registrations; } + public void setRegistrations(Set registrations) { + this.registrations = registrations; + } + @Override public int hashCode() { final int prime = 31; diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRating.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRating.java index 4951f766bc..b89cc1dae9 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRating.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRating.java @@ -1,12 +1,6 @@ package com.baeldung.manytomany.model; -import javax.persistence.Column; -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.MapsId; -import javax.persistence.Table; +import javax.persistence.*; @Entity @Table(name = "course_rating") @@ -16,12 +10,12 @@ public class CourseRating { private CourseRatingKey id; @ManyToOne - @MapsId("student_id") + @MapsId("studentId") @JoinColumn(name = "student_id") private Student student; @ManyToOne - @MapsId("course_id") + @MapsId("courseId") @JoinColumn(name = "course_id") private Course course; @@ -31,26 +25,38 @@ public class CourseRating { public CourseRating() { } - public int getRating() { - return rating; - } - - public void setRating(int rating) { - this.rating = rating; - } - public CourseRatingKey getId() { return id; } + public void setId(CourseRatingKey id) { + this.id = id; + } + 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 int getRating() { + return rating; + } + + public void setRating(int rating) { + this.rating = rating; + } + @Override public int hashCode() { final int prime = 31; diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java index 4e7430ed92..a4b2df9b07 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java @@ -1,9 +1,8 @@ package com.baeldung.manytomany.model; -import java.io.Serializable; - import javax.persistence.Column; import javax.persistence.Embeddable; +import java.io.Serializable; @Embeddable public class CourseRatingKey implements Serializable { @@ -17,14 +16,27 @@ public class CourseRatingKey implements Serializable { public CourseRatingKey() { } + public CourseRatingKey(Long studentId, Long courseId) { + this.studentId = studentId; + this.courseId = courseId; + } + public Long getStudentId() { return studentId; } + public void setStudentId(Long studentId) { + this.studentId = studentId; + } + public Long getCourseId() { return courseId; } + public void setCourseId(Long courseId) { + this.courseId = courseId; + } + @Override public int hashCode() { final int prime = 31; diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java index e1f30af883..ba914b564d 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java @@ -1,14 +1,8 @@ package com.baeldung.manytomany.model; +import javax.persistence.*; import java.time.LocalDateTime; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; - @Entity @Table(name = "course_registration") public class CourseRegistration { @@ -36,6 +30,14 @@ public class CourseRegistration { public CourseRegistration() { } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + public Student getStudent() { return student; } @@ -68,10 +70,6 @@ public class CourseRegistration { this.grade = grade; } - public Long getId() { - return id; - } - @Override public int hashCode() { final int prime = 31; diff --git a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Student.java b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Student.java index 00561593a6..53c5d88a57 100644 --- a/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Student.java +++ b/persistence-modules/spring-jpa-2/src/main/java/com/baeldung/manytomany/model/Student.java @@ -1,16 +1,9 @@ package com.baeldung.manytomany.model; +import javax.persistence.*; +import java.util.HashSet; import java.util.Set; -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; -import javax.persistence.OneToMany; -import javax.persistence.Table; - @Entity @Table(name = "student") public class Student { @@ -21,35 +14,55 @@ public class Student { @ManyToMany @JoinTable(name = "course_like", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id")) - private Set likedCourses; + private Set likedCourses = new HashSet<>(); @OneToMany(mappedBy = "student") - private Set ratings; + private Set ratings = new HashSet<>(); @OneToMany(mappedBy = "student") - private Set registrations; + private Set registrations = new HashSet<>(); // additional properties public Student() { } + public Student(Long id) { + this.id = id; + } + public Long getId() { return id; } + public void setId(Long id) { + this.id = id; + } + public Set getLikedCourses() { return likedCourses; } + public void setLikedCourses(Set likedCourses) { + this.likedCourses = likedCourses; + } + public Set getRatings() { return ratings; } + public void setRatings(Set ratings) { + this.ratings = ratings; + } + public Set getRegistrations() { return registrations; } + public void setRegistrations(Set registrations) { + this.registrations = registrations; + } + @Override public int hashCode() { final int prime = 31; diff --git a/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java b/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java index 5e4334f5d4..f5b02ec2c1 100644 --- a/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java +++ b/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java @@ -1,17 +1,27 @@ package com.baeldung.manytomany; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceContext; - +import com.baeldung.manytomany.model.Course; +import com.baeldung.manytomany.model.CourseRating; +import com.baeldung.manytomany.model.CourseRatingKey; +import com.baeldung.manytomany.model.Student; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.transaction.annotation.Transactional; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.assertThat; @RunWith(SpringRunner.class) @ContextConfiguration(classes = ManyToManyTestConfiguration.class) @DirtiesContext +@Transactional public class ManyToManyIntegrationTest { @PersistenceContext @@ -21,4 +31,25 @@ public class ManyToManyIntegrationTest { public void contextStarted() { } + @Test + public void whenCourseRatingPersisted_thenCorrect() { + Student student = new Student(101L); + entityManager.persist(student); + + Course course = new Course(201L); + entityManager.persist(course); + + CourseRating courseRating = new CourseRating(); + courseRating.setId(new CourseRatingKey()); + courseRating.setStudent(student); + courseRating.setCourse(course); + courseRating.setRating(100); + entityManager.persist(courseRating); + + CourseRating persistedCourseRating = entityManager.find(CourseRating.class, new CourseRatingKey(101L, 201L)); + + assertThat(persistedCourseRating, notNullValue()); + assertThat(persistedCourseRating.getStudent().getId(), is(101L)); + assertThat(persistedCourseRating.getCourse().getId(), is(201L)); + } } diff --git a/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java b/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java index 1cc3621f0d..fa12828074 100644 --- a/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java +++ b/persistence-modules/spring-jpa-2/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java @@ -1,20 +1,21 @@ package com.baeldung.manytomany; -import java.util.HashMap; -import java.util.Map; - -import javax.sql.DataSource; - import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; +import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; +import java.util.HashMap; +import java.util.Map; + @Configuration @PropertySource("manytomany/test.properties") public class ManyToManyTestConfiguration { @@ -23,8 +24,8 @@ public class ManyToManyTestConfiguration { public DataSource dataSource() { EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder(); return dbBuilder.setType(EmbeddedDatabaseType.H2) - .addScript("classpath:/manytomany/db.sql") - .build(); + .addScript("classpath:/manytomany/db.sql") + .build(); } @Bean @@ -44,6 +45,13 @@ public class ManyToManyTestConfiguration { return result; } + @Bean + JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) { + JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory); + return transactionManager; + } + public JpaVendorAdapter jpaVendorAdapter() { return new HibernateJpaVendorAdapter(); }