BAEL-4577: Fix @MapsIs usage and add integration test (#9930)

This commit is contained in:
kwoyke 2020-08-28 08:01:12 +02:00 committed by GitHub
parent 93c0a00a48
commit 482357866e
7 changed files with 149 additions and 62 deletions

View File

@ -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<Student> likes;
private Set<Student> likes = new HashSet<>();
@OneToMany(mappedBy = "course")
private Set<CourseRating> ratings;
private Set<CourseRating> ratings = new HashSet<>();
@OneToMany(mappedBy = "course")
private Set<CourseRegistration> registrations;
private Set<CourseRegistration> 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<Student> getLikes() {
return likes;
}
public void setLikes(Set<Student> likes) {
this.likes = likes;
}
public Set<CourseRating> getRatings() {
return ratings;
}
public void setRatings(Set<CourseRating> ratings) {
this.ratings = ratings;
}
public Set<CourseRegistration> getRegistrations() {
return registrations;
}
public void setRegistrations(Set<CourseRegistration> registrations) {
this.registrations = registrations;
}
@Override
public int hashCode() {
final int prime = 31;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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<Course> likedCourses;
private Set<Course> likedCourses = new HashSet<>();
@OneToMany(mappedBy = "student")
private Set<CourseRating> ratings;
private Set<CourseRating> ratings = new HashSet<>();
@OneToMany(mappedBy = "student")
private Set<CourseRegistration> registrations;
private Set<CourseRegistration> 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<Course> getLikedCourses() {
return likedCourses;
}
public void setLikedCourses(Set<Course> likedCourses) {
this.likedCourses = likedCourses;
}
public Set<CourseRating> getRatings() {
return ratings;
}
public void setRatings(Set<CourseRating> ratings) {
this.ratings = ratings;
}
public Set<CourseRegistration> getRegistrations() {
return registrations;
}
public void setRegistrations(Set<CourseRegistration> registrations) {
this.registrations = registrations;
}
@Override
public int hashCode() {
final int prime = 31;

View File

@ -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));
}
}

View File

@ -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();
}