Skip to content

Commit d2a4341

Browse files
committed
bael-2190 - JPA many-to-many: adding tests and changing package
1 parent 9c434f2 commit d2a4341

9 files changed

Lines changed: 148 additions & 5 deletions

File tree

persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Course.java renamed to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Course.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
package com.baeldung.manytomany.extracolumn.model;
1+
package com.baeldung.manytomany.model;
22

33
import java.util.Set;
44

5+
import javax.persistence.Column;
56
import javax.persistence.Entity;
67
import javax.persistence.Id;
78
import javax.persistence.ManyToMany;
89
import javax.persistence.OneToMany;
10+
import javax.persistence.Table;
911

1012
@Entity
13+
@Table(name = "course")
1114
public class Course {
1215

1316
@Id
17+
@Column(name = "id")
1418
private Long id;
1519

1620
@ManyToMany(mappedBy = "likedCourses")

persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRating.java renamed to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRating.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
package com.baeldung.manytomany.extracolumn.model;
1+
package com.baeldung.manytomany.model;
22

3+
import javax.persistence.Column;
34
import javax.persistence.EmbeddedId;
45
import javax.persistence.Entity;
56
import javax.persistence.JoinColumn;
67
import javax.persistence.ManyToOne;
78
import javax.persistence.MapsId;
9+
import javax.persistence.Table;
810

911
@Entity
12+
@Table(name = "course_rating")
1013
public class CourseRating {
1114

1215
@EmbeddedId
@@ -22,6 +25,7 @@ public class CourseRating {
2225
@JoinColumn(name = "course_id")
2326
private Course course;
2427

28+
@Column(name = "rating")
2529
private int rating;
2630

2731
public CourseRating() {

persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRatingKey.java renamed to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRatingKey.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.manytomany.extracolumn.model;
1+
package com.baeldung.manytomany.model;
22

33
import java.io.Serializable;
44

persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/CourseRegistration.java renamed to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/CourseRegistration.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
package com.baeldung.manytomany.extracolumn.model;
1+
package com.baeldung.manytomany.model;
22

33
import java.time.LocalDateTime;
44

5+
import javax.persistence.Column;
56
import javax.persistence.Entity;
67
import javax.persistence.Id;
78
import javax.persistence.JoinColumn;
89
import javax.persistence.ManyToOne;
10+
import javax.persistence.Table;
911

1012
@Entity
13+
@Table(name = "course_registration")
1114
public class CourseRegistration {
1215

1316
@Id
17+
@Column(name = "id")
1418
private Long id;
1519

1620
@ManyToOne
@@ -21,8 +25,10 @@ public class CourseRegistration {
2125
@JoinColumn(name = "course_id")
2226
private Course course;
2327

28+
@Column(name = "registered_at")
2429
private LocalDateTime registeredAt;
2530

31+
@Column(name = "grade")
2632
private int grade;
2733

2834
// additional properties

persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/extracolumn/model/Student.java renamed to persistence-modules/spring-jpa/src/main/java/com/baeldung/manytomany/model/Student.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
package com.baeldung.manytomany.extracolumn.model;
1+
package com.baeldung.manytomany.model;
22

33
import java.util.Set;
44

5+
import javax.persistence.Column;
56
import javax.persistence.Entity;
67
import javax.persistence.Id;
78
import javax.persistence.JoinColumn;
89
import javax.persistence.JoinTable;
910
import javax.persistence.ManyToMany;
1011
import javax.persistence.OneToMany;
12+
import javax.persistence.Table;
1113

1214
@Entity
15+
@Table(name = "student")
1316
public class Student {
1417

1518
@Id
19+
@Column(name = "id")
1620
private Long id;
1721

1822
@ManyToMany
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.manytomany;
2+
3+
import javax.persistence.EntityManager;
4+
import javax.persistence.PersistenceContext;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.test.annotation.DirtiesContext;
9+
import org.springframework.test.context.ContextConfiguration;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
@RunWith(SpringRunner.class)
13+
@ContextConfiguration(classes = ManyToManyTestConfiguration.class)
14+
@DirtiesContext
15+
public class ManyToManyIntegrationTest {
16+
17+
@PersistenceContext
18+
EntityManager entityManager;
19+
20+
@Test
21+
public void contextStarted() {
22+
}
23+
24+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.manytomany;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import javax.sql.DataSource;
7+
8+
import org.springframework.beans.factory.annotation.Value;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.PropertySource;
12+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
13+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
14+
import org.springframework.orm.jpa.JpaVendorAdapter;
15+
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
16+
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
17+
18+
@Configuration
19+
@PropertySource("classpath:/manytomany/test.properties")
20+
public class ManyToManyTestConfiguration {
21+
22+
@Bean
23+
public DataSource dataSource() {
24+
EmbeddedDatabaseBuilder dbBuilder = new EmbeddedDatabaseBuilder();
25+
return dbBuilder.setType(EmbeddedDatabaseType.H2)
26+
.addScript("classpath:/manytomany/db.sql")
27+
.build();
28+
}
29+
30+
@Bean
31+
public LocalContainerEntityManagerFactoryBean entityManagerFactory(@Value("${hibernate.hbm2ddl.auto}") String hbm2ddlType, @Value("${hibernate.dialect}") String dialect, @Value("${hibernate.show_sql}") boolean showSql) {
32+
LocalContainerEntityManagerFactoryBean result = new LocalContainerEntityManagerFactoryBean();
33+
34+
result.setDataSource(dataSource());
35+
result.setPackagesToScan("com.baeldung.manytomany.model");
36+
result.setJpaVendorAdapter(jpaVendorAdapter());
37+
38+
Map<String, Object> jpaProperties = new HashMap<>();
39+
jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddlType);
40+
jpaProperties.put("hibernate.dialect", dialect);
41+
jpaProperties.put("hibernate.show_sql", showSql);
42+
result.setJpaPropertyMap(jpaProperties);
43+
44+
return result;
45+
}
46+
47+
public JpaVendorAdapter jpaVendorAdapter() {
48+
return new HibernateJpaVendorAdapter();
49+
}
50+
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
CREATE TABLE course (
2+
id bigint(20) NOT NULL,
3+
PRIMARY KEY (id)
4+
);
5+
6+
7+
CREATE TABLE student (
8+
id bigint(20) NOT NULL,
9+
PRIMARY KEY (id)
10+
);
11+
12+
13+
CREATE TABLE course_like (
14+
student_id bigint(20) NOT NULL,
15+
course_id bigint(20) NOT NULL,
16+
PRIMARY KEY (student_id, course_id),
17+
CONSTRAINT fk_course_like__student FOREIGN KEY (student_id) REFERENCES student (id),
18+
CONSTRAINT fk_course_like__course FOREIGN KEY (course_id) REFERENCES course (id)
19+
);
20+
21+
22+
23+
CREATE TABLE course_rating (
24+
course_id bigint(20) NOT NULL,
25+
student_id bigint(20) NOT NULL,
26+
rating int(11) NOT NULL,
27+
PRIMARY KEY (course_id, student_id),
28+
CONSTRAINT fk_course_rating__student FOREIGN KEY (student_id) REFERENCES student (id),
29+
CONSTRAINT fk_course_rating__course FOREIGN KEY (course_id) REFERENCES course (id)
30+
);
31+
32+
33+
34+
CREATE TABLE course_registration (
35+
id bigint(20) NOT NULL,
36+
grade int(11),
37+
registered_at datetime NOT NULL,
38+
course_id bigint(20) NOT NULL,
39+
student_id bigint(20) NOT NULL,
40+
PRIMARY KEY (id),
41+
CONSTRAINT fk_course_registration__student FOREIGN KEY (student_id) REFERENCES student (id),
42+
CONSTRAINT fk_course_registration__course FOREIGN KEY (course_id) REFERENCES course (id)
43+
);
44+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
jdbc.driverClassName=org.h2.Driver
2+
jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
3+
4+
hibernate.dialect=org.hibernate.dialect.H2Dialect
5+
hibernate.show_sql=true
6+
hibernate.hbm2ddl.auto=validate

0 commit comments

Comments
 (0)