bael-2190 - JPA many-to-many: adding tests and changing package
This commit is contained in:
parent
9c434f2ad8
commit
d2a4341d97
|
@ -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")
|
|
@ -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() {
|
|
@ -1,4 +1,4 @@
|
|||
package com.baeldung.manytomany.extracolumn.model;
|
||||
package com.baeldung.manytomany.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
|
@ -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
|
|
@ -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
|
|
@ -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() {
|
||||
}
|
||||
|
||||
}
|
|
@ -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<String, Object> 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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)
|
||||
);
|
||||
|
|
@ -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
|
Loading…
Reference in New Issue