BAEL-4577: Fix @MapsIs usage and add integration test (#9930)
This commit is contained in:
parent
93c0a00a48
commit
482357866e
@ -1,14 +1,9 @@
|
|||||||
package com.baeldung.manytomany.model;
|
package com.baeldung.manytomany.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
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
|
@Entity
|
||||||
@Table(name = "course")
|
@Table(name = "course")
|
||||||
public class Course {
|
public class Course {
|
||||||
@ -18,31 +13,55 @@ public class Course {
|
|||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@ManyToMany(mappedBy = "likedCourses")
|
@ManyToMany(mappedBy = "likedCourses")
|
||||||
private Set<Student> likes;
|
private Set<Student> likes = new HashSet<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "course")
|
@OneToMany(mappedBy = "course")
|
||||||
private Set<CourseRating> ratings;
|
private Set<CourseRating> ratings = new HashSet<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "course")
|
@OneToMany(mappedBy = "course")
|
||||||
private Set<CourseRegistration> registrations;
|
private Set<CourseRegistration> registrations = new HashSet<>();
|
||||||
|
|
||||||
// additional properties
|
// additional properties
|
||||||
|
|
||||||
public Course() {
|
public Course() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Course(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
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() {
|
public Set<CourseRating> getRatings() {
|
||||||
return ratings;
|
return ratings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRatings(Set<CourseRating> ratings) {
|
||||||
|
this.ratings = ratings;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<CourseRegistration> getRegistrations() {
|
public Set<CourseRegistration> getRegistrations() {
|
||||||
return registrations;
|
return registrations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRegistrations(Set<CourseRegistration> registrations) {
|
||||||
|
this.registrations = registrations;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
@ -1,12 +1,6 @@
|
|||||||
package com.baeldung.manytomany.model;
|
package com.baeldung.manytomany.model;
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.*;
|
||||||
import javax.persistence.EmbeddedId;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.JoinColumn;
|
|
||||||
import javax.persistence.ManyToOne;
|
|
||||||
import javax.persistence.MapsId;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "course_rating")
|
@Table(name = "course_rating")
|
||||||
@ -16,12 +10,12 @@ public class CourseRating {
|
|||||||
private CourseRatingKey id;
|
private CourseRatingKey id;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@MapsId("student_id")
|
@MapsId("studentId")
|
||||||
@JoinColumn(name = "student_id")
|
@JoinColumn(name = "student_id")
|
||||||
private Student student;
|
private Student student;
|
||||||
|
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
@MapsId("course_id")
|
@MapsId("courseId")
|
||||||
@JoinColumn(name = "course_id")
|
@JoinColumn(name = "course_id")
|
||||||
private Course course;
|
private Course course;
|
||||||
|
|
||||||
@ -31,26 +25,38 @@ public class CourseRating {
|
|||||||
public CourseRating() {
|
public CourseRating() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getRating() {
|
|
||||||
return rating;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRating(int rating) {
|
|
||||||
this.rating = rating;
|
|
||||||
}
|
|
||||||
|
|
||||||
public CourseRatingKey getId() {
|
public CourseRatingKey getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setId(CourseRatingKey id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Student getStudent() {
|
public Student getStudent() {
|
||||||
return student;
|
return student;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setStudent(Student student) {
|
||||||
|
this.student = student;
|
||||||
|
}
|
||||||
|
|
||||||
public Course getCourse() {
|
public Course getCourse() {
|
||||||
return course;
|
return course;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCourse(Course course) {
|
||||||
|
this.course = course;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRating() {
|
||||||
|
return rating;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRating(int rating) {
|
||||||
|
this.rating = rating;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package com.baeldung.manytomany.model;
|
package com.baeldung.manytomany.model;
|
||||||
|
|
||||||
import java.io.Serializable;
|
|
||||||
|
|
||||||
import javax.persistence.Column;
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Embeddable;
|
import javax.persistence.Embeddable;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
@Embeddable
|
@Embeddable
|
||||||
public class CourseRatingKey implements Serializable {
|
public class CourseRatingKey implements Serializable {
|
||||||
@ -17,14 +16,27 @@ public class CourseRatingKey implements Serializable {
|
|||||||
public CourseRatingKey() {
|
public CourseRatingKey() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CourseRatingKey(Long studentId, Long courseId) {
|
||||||
|
this.studentId = studentId;
|
||||||
|
this.courseId = courseId;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getStudentId() {
|
public Long getStudentId() {
|
||||||
return studentId;
|
return studentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setStudentId(Long studentId) {
|
||||||
|
this.studentId = studentId;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getCourseId() {
|
public Long getCourseId() {
|
||||||
return courseId;
|
return courseId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCourseId(Long courseId) {
|
||||||
|
this.courseId = courseId;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
@ -1,14 +1,8 @@
|
|||||||
package com.baeldung.manytomany.model;
|
package com.baeldung.manytomany.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
import java.time.LocalDateTime;
|
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
|
@Entity
|
||||||
@Table(name = "course_registration")
|
@Table(name = "course_registration")
|
||||||
public class CourseRegistration {
|
public class CourseRegistration {
|
||||||
@ -36,6 +30,14 @@ public class CourseRegistration {
|
|||||||
public CourseRegistration() {
|
public CourseRegistration() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Student getStudent() {
|
public Student getStudent() {
|
||||||
return student;
|
return student;
|
||||||
}
|
}
|
||||||
@ -68,10 +70,6 @@ public class CourseRegistration {
|
|||||||
this.grade = grade;
|
this.grade = grade;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
@ -1,16 +1,9 @@
|
|||||||
package com.baeldung.manytomany.model;
|
package com.baeldung.manytomany.model;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
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
|
@Entity
|
||||||
@Table(name = "student")
|
@Table(name = "student")
|
||||||
public class Student {
|
public class Student {
|
||||||
@ -21,35 +14,55 @@ public class Student {
|
|||||||
|
|
||||||
@ManyToMany
|
@ManyToMany
|
||||||
@JoinTable(name = "course_like", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id"))
|
@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")
|
@OneToMany(mappedBy = "student")
|
||||||
private Set<CourseRating> ratings;
|
private Set<CourseRating> ratings = new HashSet<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "student")
|
@OneToMany(mappedBy = "student")
|
||||||
private Set<CourseRegistration> registrations;
|
private Set<CourseRegistration> registrations = new HashSet<>();
|
||||||
|
|
||||||
// additional properties
|
// additional properties
|
||||||
|
|
||||||
public Student() {
|
public Student() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Student(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Long getId() {
|
public Long getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<Course> getLikedCourses() {
|
public Set<Course> getLikedCourses() {
|
||||||
return likedCourses;
|
return likedCourses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setLikedCourses(Set<Course> likedCourses) {
|
||||||
|
this.likedCourses = likedCourses;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<CourseRating> getRatings() {
|
public Set<CourseRating> getRatings() {
|
||||||
return ratings;
|
return ratings;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRatings(Set<CourseRating> ratings) {
|
||||||
|
this.ratings = ratings;
|
||||||
|
}
|
||||||
|
|
||||||
public Set<CourseRegistration> getRegistrations() {
|
public Set<CourseRegistration> getRegistrations() {
|
||||||
return registrations;
|
return registrations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setRegistrations(Set<CourseRegistration> registrations) {
|
||||||
|
this.registrations = registrations;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
final int prime = 31;
|
final int prime = 31;
|
||||||
|
@ -1,17 +1,27 @@
|
|||||||
package com.baeldung.manytomany;
|
package com.baeldung.manytomany;
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
import com.baeldung.manytomany.model.Course;
|
||||||
import javax.persistence.PersistenceContext;
|
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.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
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)
|
@RunWith(SpringRunner.class)
|
||||||
@ContextConfiguration(classes = ManyToManyTestConfiguration.class)
|
@ContextConfiguration(classes = ManyToManyTestConfiguration.class)
|
||||||
@DirtiesContext
|
@DirtiesContext
|
||||||
|
@Transactional
|
||||||
public class ManyToManyIntegrationTest {
|
public class ManyToManyIntegrationTest {
|
||||||
|
|
||||||
@PersistenceContext
|
@PersistenceContext
|
||||||
@ -21,4 +31,25 @@ public class ManyToManyIntegrationTest {
|
|||||||
public void contextStarted() {
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,21 @@
|
|||||||
package com.baeldung.manytomany;
|
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.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||||
|
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||||
import org.springframework.orm.jpa.JpaVendorAdapter;
|
import org.springframework.orm.jpa.JpaVendorAdapter;
|
||||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@PropertySource("manytomany/test.properties")
|
@PropertySource("manytomany/test.properties")
|
||||||
public class ManyToManyTestConfiguration {
|
public class ManyToManyTestConfiguration {
|
||||||
@ -44,6 +45,13 @@ public class ManyToManyTestConfiguration {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
||||||
|
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||||
|
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
||||||
|
return transactionManager;
|
||||||
|
}
|
||||||
|
|
||||||
public JpaVendorAdapter jpaVendorAdapter() {
|
public JpaVendorAdapter jpaVendorAdapter() {
|
||||||
return new HibernateJpaVendorAdapter();
|
return new HibernateJpaVendorAdapter();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user