From 9c434f2ad80e0bca8de3b2921557b4d3ece4cba1 Mon Sep 17 00:00:00 2001 From: fanatixan Date: Sat, 3 Nov 2018 18:11:38 +0100 Subject: [PATCH 1/2] bael-2190 --- .../manytomany/extracolumn/model/Course.java | 67 +++++++++++++ .../extracolumn/model/CourseRating.java | 75 +++++++++++++++ .../extracolumn/model/CourseRatingKey.java | 59 ++++++++++++ .../extracolumn/model/CourseRegistration.java | 94 +++++++++++++++++++ .../manytomany/extracolumn/model/Student.java | 74 +++++++++++++++ 5 files changed, 369 insertions(+) create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java create mode 100644 persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java new file mode 100644 index 0000000000..4849165c57 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java @@ -0,0 +1,67 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; + +@Entity +public class Course { + + @Id + private Long id; + + @ManyToMany(mappedBy = "likedCourses") + private Set likes; + + @OneToMany(mappedBy = "course") + private Set ratings; + + @OneToMany(mappedBy = "course") + private Set registrations; + + // additional properties + + public Course() { + } + + public Long getId() { + return id; + } + + public Set getRatings() { + return ratings; + } + + public Set getRegistrations() { + return registrations; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Course other = (Course) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java new file mode 100644 index 0000000000..1b6c9d8b2c --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java @@ -0,0 +1,75 @@ +package com.baeldung.manytomany.extracolumn.model; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; + +@Entity +public class CourseRating { + + @EmbeddedId + private CourseRatingKey id; + + @ManyToOne + @MapsId("student_id") + @JoinColumn(name = "student_id") + private Student student; + + @ManyToOne + @MapsId("course_id") + @JoinColumn(name = "course_id") + private Course course; + + private int rating; + + public CourseRating() { + } + + public int getRating() { + return rating; + } + + public void setRating(int rating) { + this.rating = rating; + } + + public CourseRatingKey getId() { + return id; + } + + public Student getStudent() { + return student; + } + + public Course getCourse() { + return course; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CourseRating other = (CourseRating) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java new file mode 100644 index 0000000000..6638ae6968 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java @@ -0,0 +1,59 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class CourseRatingKey implements Serializable { + + @Column(name = "student_id") + private Long studentId; + + @Column(name = "course_id") + private Long courseId; + + public CourseRatingKey() { + } + + public Long getStudentId() { + return studentId; + } + + public Long getCourseId() { + return courseId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((courseId == null) ? 0 : courseId.hashCode()); + result = prime * result + ((studentId == null) ? 0 : studentId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CourseRatingKey other = (CourseRatingKey) obj; + if (courseId == null) { + if (other.courseId != null) + return false; + } else if (!courseId.equals(other.courseId)) + return false; + if (studentId == null) { + if (other.studentId != null) + return false; + } else if (!studentId.equals(other.studentId)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java new file mode 100644 index 0000000000..225968dba4 --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java @@ -0,0 +1,94 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.time.LocalDateTime; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; + +@Entity +public class CourseRegistration { + + @Id + private Long id; + + @ManyToOne + @JoinColumn(name = "student_id") + private Student student; + + @ManyToOne + @JoinColumn(name = "course_id") + private Course course; + + private LocalDateTime registeredAt; + + private int grade; + + // additional properties + + public CourseRegistration() { + } + + 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 LocalDateTime getRegisteredAt() { + return registeredAt; + } + + public void setRegisteredAt(LocalDateTime registeredAt) { + this.registeredAt = registeredAt; + } + + public int getGrade() { + return grade; + } + + public void setGrade(int grade) { + this.grade = grade; + } + + public Long getId() { + return id; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + CourseRegistration other = (CourseRegistration) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java new file mode 100644 index 0000000000..6bc8271e7e --- /dev/null +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java @@ -0,0 +1,74 @@ +package com.baeldung.manytomany.extracolumn.model; + +import java.util.Set; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.OneToMany; + +@Entity +public class Student { + + @Id + private Long id; + + @ManyToMany + @JoinTable(name = "course_like", joinColumns = @JoinColumn(name = "student_id"), inverseJoinColumns = @JoinColumn(name = "course_id")) + private Set likedCourses; + + @OneToMany(mappedBy = "student") + private Set ratings; + + @OneToMany(mappedBy = "student") + private Set registrations; + + // additional properties + + public Student() { + } + + public Long getId() { + return id; + } + + public Set getLikedCourses() { + return likedCourses; + } + + public Set getRatings() { + return ratings; + } + + public Set getRegistrations() { + return registrations; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Student other = (Student) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + return true; + } + +} From d2a4341d9763cf1c062b75b31cf597af8601aecc Mon Sep 17 00:00:00 2001 From: fanatixan Date: Sun, 4 Nov 2018 17:29:22 +0100 Subject: [PATCH 2/2] bael-2190 - JPA many-to-many: adding tests and changing package --- .../{extracolumn => }/model/Course.java | 6 ++- .../{extracolumn => }/model/CourseRating.java | 6 ++- .../model/CourseRatingKey.java | 2 +- .../model/CourseRegistration.java | 8 ++- .../{extracolumn => }/model/Student.java | 6 ++- .../manytomany/ManyToManyIntegrationTest.java | 24 +++++++++ .../ManyToManyTestConfiguration.java | 51 +++++++++++++++++++ .../src/test/resources/manytomany/db.sql | 44 ++++++++++++++++ .../test/resources/manytomany/test.properties | 6 +++ 9 files changed, 148 insertions(+), 5 deletions(-) rename persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/{extracolumn => }/model/Course.java (90%) rename persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/{extracolumn => }/model/CourseRating.java (90%) rename persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/{extracolumn => }/model/CourseRatingKey.java (96%) rename persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/{extracolumn => }/model/CourseRegistration.java (89%) rename persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/{extracolumn => }/model/Student.java (91%) create mode 100644 persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java create mode 100644 persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java create mode 100644 persistence-modules/spring-jpa/src/test/resources/manytomany/db.sql create mode 100644 persistence-modules/spring-jpa/src/test/resources/manytomany/test.properties diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Course.java similarity index 90% rename from persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Course.java index 4849165c57..bdfb8e890f 100644 --- a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Course.java @@ -1,16 +1,20 @@ -package com.baeldung.manytomany.extracolumn.model; +package com.baeldung.manytomany.model; 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 { @Id + @Column(name = "id") private Long id; @ManyToMany(mappedBy = "likedCourses") diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRating.java similarity index 90% rename from persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRating.java index 1b6c9d8b2c..4951f766bc 100644 --- a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRating.java @@ -1,12 +1,15 @@ -package com.baeldung.manytomany.extracolumn.model; +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; @Entity +@Table(name = "course_rating") public class CourseRating { @EmbeddedId @@ -22,6 +25,7 @@ public class CourseRating { @JoinColumn(name = "course_id") private Course course; + @Column(name = "rating") private int rating; public CourseRating() { diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java similarity index 96% rename from persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java index 6638ae6968..4e7430ed92 100644 --- a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java @@ -1,4 +1,4 @@ -package com.baeldung.manytomany.extracolumn.model; +package com.baeldung.manytomany.model; import java.io.Serializable; diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java similarity index 89% rename from persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java index 225968dba4..e1f30af883 100644 --- a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java @@ -1,16 +1,20 @@ -package com.baeldung.manytomany.extracolumn.model; +package com.baeldung.manytomany.model; 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 { @Id + @Column(name = "id") private Long id; @ManyToOne @@ -21,8 +25,10 @@ public class CourseRegistration { @JoinColumn(name = "course_id") private Course course; + @Column(name = "registered_at") private LocalDateTime registeredAt; + @Column(name = "grade") private int grade; // additional properties diff --git a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Student.java similarity index 91% rename from persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java rename to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Student.java index 6bc8271e7e..00561593a6 100644 --- a/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Student.java @@ -1,18 +1,22 @@ -package com.baeldung.manytomany.extracolumn.model; +package com.baeldung.manytomany.model; 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 { @Id + @Column(name = "id") private Long id; @ManyToMany diff --git a/persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java new file mode 100644 index 0000000000..5e4334f5d4 --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyIntegrationTest.java @@ -0,0 +1,24 @@ +package com.baeldung.manytomany; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; + +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; + +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = ManyToManyTestConfiguration.class) +@DirtiesContext +public class ManyToManyIntegrationTest { + + @PersistenceContext + EntityManager entityManager; + + @Test + public void contextStarted() { + } + +} diff --git a/persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java b/persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java new file mode 100644 index 0000000000..f4635b563a --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/java/com/baeldung/manytomany/ManyToManyTestConfiguration.java @@ -0,0 +1,51 @@ +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.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; + +@Configuration +@PropertySource("classpath:/manytomany/test.properties") +public class ManyToManyTestConfiguration { + + @Bean + public DataSource dataSource() { + EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder(); + return dbBuilder.setType(EmbeddedDatabaseType.H2) + .addScript("classpath:/manytomany/db.sql") + .build(); + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${hibernate.hbm2ddl.auto}") String hbm2ddlType, @Value("${hibernate.dialect}") String dialect, @Value("${hibernate.show_sql}") boolean showSql) { + LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean(); + + result.setDataSource(dataSource()); + result.setPackagesToScan("com.baeldung.manytomany.model"); + result.setJpaVendorAdapter(jpaVendorAdapter()); + + Map jpaProperties = new HashMap<>(); + jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddlType); + jpaProperties.put("hibernate.dialect", dialect); + jpaProperties.put("hibernate.show_sql", showSql); + result.setJpaPropertyMap(jpaProperties); + + return result; + } + + public JpaVendorAdapter jpaVendorAdapter() { + return new HibernateJpaVendorAdapter(); + } + +} diff --git a/persistence-modules/spring-jpa/src/test/resources/manytomany/db.sql b/persistence-modules/spring-jpa/src/test/resources/manytomany/db.sql new file mode 100644 index 0000000000..02905e41ee --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/resources/manytomany/db.sql @@ -0,0 +1,44 @@ +CREATE TABLE course ( + id bigint(20) NOT NULL, + PRIMARY KEY (id) +); + + +CREATE TABLE student ( + id bigint(20) NOT NULL, + PRIMARY KEY (id) +); + + +CREATE TABLE course_like ( + student_id bigint(20) NOT NULL, + course_id bigint(20) NOT NULL, + PRIMARY KEY (student_id, course_id), + CONSTRAINT fk_course_like__student FOREIGN KEY (student_id) REFERENCES student (id), + CONSTRAINT fk_course_like__course FOREIGN KEY (course_id) REFERENCES course (id) +); + + + +CREATE TABLE course_rating ( + course_id bigint(20) NOT NULL, + student_id bigint(20) NOT NULL, + rating int(11) NOT NULL, + PRIMARY KEY (course_id, student_id), + CONSTRAINT fk_course_rating__student FOREIGN KEY (student_id) REFERENCES student (id), + CONSTRAINT fk_course_rating__course FOREIGN KEY (course_id) REFERENCES course (id) +); + + + +CREATE TABLE course_registration ( + id bigint(20) NOT NULL, + grade int(11), + registered_at datetime NOT NULL, + course_id bigint(20) NOT NULL, + student_id bigint(20) NOT NULL, + PRIMARY KEY (id), + CONSTRAINT fk_course_registration__student FOREIGN KEY (student_id) REFERENCES student (id), + CONSTRAINT fk_course_registration__course FOREIGN KEY (course_id) REFERENCES course (id) +); + diff --git a/persistence-modules/spring-jpa/src/test/resources/manytomany/test.properties b/persistence-modules/spring-jpa/src/test/resources/manytomany/test.properties new file mode 100644 index 0000000000..9e4236a6c2 --- /dev/null +++ b/persistence-modules/spring-jpa/src/test/resources/manytomany/test.properties @@ -0,0 +1,6 @@ +jdbc.driverClassName=org.h2.Driver +jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1 + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.show_sql=true +hibernate.hbm2ddl.auto=validate