Skip to content

Commit 6bdcebb

Browse files
authored
Add missing metadata for Hibernate Reactive (needed for some collections mappings) (#489)
* Add missing metadata for Hibernate Reactive (needed for some collections mappings) * Fix failures * Fixed checkstyle
1 parent 6cd6c93 commit 6bdcebb

File tree

6 files changed

+305
-0
lines changed

6 files changed

+305
-0
lines changed

metadata/org.hibernate.reactive/hibernate-reactive-core/2.0.0.Final/reflect-config.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,5 +136,22 @@
136136
"condition": {
137137
"typeReachable": "org.hibernate.reactive.pool.impl.DefaultSqlClientPool"
138138
}
139+
},
140+
{
141+
"name": "org.hibernate.reactive.persister.collection.impl.ReactiveBasicCollectionPersister",
142+
"queryAllPublicMethods": true,
143+
"condition": {
144+
"typeReachable": "org.hibernate.reactive.session.impl.ReactiveSessionFactoryImpl"
145+
},
146+
"methods": [
147+
{
148+
"name": "<init>",
149+
"parameterTypes": [
150+
"org.hibernate.mapping.Collection",
151+
"org.hibernate.cache.spi.access.CollectionDataAccess",
152+
"org.hibernate.metamodel.spi.RuntimeModelCreationContext"
153+
]
154+
}
155+
]
139156
}
140157
]

tests/src/org.hibernate.reactive/hibernate-reactive-core/2.0.0.Final/src/test/java/org_hibernate_reactive/hibernate_reactive_core/HibernateReactiveCoreTest.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import org.slf4j.LoggerFactory;
1717
import org_hibernate_reactive.hibernate_reactive_core.entity.Author;
1818
import org_hibernate_reactive.hibernate_reactive_core.entity.Book;
19+
import org_hibernate_reactive.hibernate_reactive_core.entity.Course;
20+
import org_hibernate_reactive.hibernate_reactive_core.entity.Student;
1921

2022
import java.io.IOException;
2123
import java.sql.Connection;
@@ -26,6 +28,7 @@
2628
import java.util.List;
2729
import java.util.Map;
2830
import java.util.Properties;
31+
import java.util.Set;
2932
import java.util.concurrent.Callable;
3033

3134
import static java.time.Month.JANUARY;
@@ -158,4 +161,62 @@ void testSaveAndLoad() {
158161
Tuple.tuple(book1.getTitle(), author1.getName()),
159162
Tuple.tuple(book2.getTitle(), author2.getName()));
160163
}
164+
165+
/**
166+
* Tests @{@link jakarta.persistence.ManyToMany} and @{@link jakarta.persistence.ElementCollection} relations with Hibernate Reactive.
167+
*/
168+
@Test
169+
void testCollections() {
170+
final Course languageCourse = new Course("English");
171+
languageCourse.setNotes(List.of("Starting in December"));
172+
final Course mathCourse = new Course("Mathematics");
173+
174+
// Save courses
175+
factory.withTransaction((session, tx) -> session.persist(languageCourse, mathCourse))
176+
.toCompletableFuture()
177+
.join();
178+
179+
// retrieve a Course
180+
Course loadedLanguageCourse = factory.withSession(session -> session.find(Course.class, languageCourse.getId()))
181+
.toCompletableFuture()
182+
.join();
183+
assertThat(loadedLanguageCourse).isNotNull();
184+
assertThat(loadedLanguageCourse.getStudents().size()).isEqualTo(0);
185+
assertThat(loadedLanguageCourse.getNotes().size()).isEqualTo(1);
186+
187+
Course loadedMathCourse = factory.withSession(session -> session.find(Course.class, mathCourse.getId()))
188+
.toCompletableFuture()
189+
.join();
190+
assertThat(loadedMathCourse).isNotNull();
191+
assertThat(loadedMathCourse.getStudents().size()).isEqualTo(0);
192+
assertThat(loadedMathCourse.getNotes().size()).isEqualTo(0);
193+
194+
final Student student = new Student("Peter", Set.of(mathCourse));
195+
// Save Student
196+
factory.withTransaction((session, tx) -> session.persist(student))
197+
.toCompletableFuture()
198+
.join();
199+
Long id = student.getId();
200+
assertThat(id).isNotNull();
201+
202+
// retrieve a Student and verify that student has course assigned
203+
Student loadedStudent = factory.withSession(session -> session.find(Student.class, student.getId()))
204+
.toCompletableFuture()
205+
.join();
206+
assertThat(loadedStudent).isNotNull();
207+
assertThat(loadedStudent.getName()).isEqualTo("Peter");
208+
assertThat(loadedStudent.getCourses().size()).isEqualTo(1);
209+
210+
loadedLanguageCourse = factory.withSession(session -> session.find(Course.class, languageCourse.getId()))
211+
.toCompletableFuture()
212+
.join();
213+
assertThat(loadedLanguageCourse).isNotNull();
214+
assertThat(loadedLanguageCourse.getStudents().size()).isEqualTo(0);
215+
216+
loadedMathCourse = factory.withSession(session -> session.find(Course.class, mathCourse.getId()))
217+
.toCompletableFuture()
218+
.join();
219+
assertThat(loadedMathCourse).isNotNull();
220+
assertThat(loadedMathCourse.getStudents().size()).isEqualTo(1);
221+
}
161222
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright and related rights waived via CC0
3+
*
4+
* You should have received a copy of the CC0 legalcode along with this
5+
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
6+
*/
7+
package org_hibernate_reactive.hibernate_reactive_core.entity;
8+
9+
import jakarta.persistence.ElementCollection;
10+
import jakarta.persistence.Entity;
11+
import jakarta.persistence.FetchType;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.Id;
14+
import jakarta.persistence.ManyToMany;
15+
import jakarta.persistence.Table;
16+
17+
import java.util.HashSet;
18+
import java.util.List;
19+
import java.util.Set;
20+
21+
@Entity
22+
@Table(name = "course")
23+
public class Course {
24+
25+
@Id
26+
@GeneratedValue
27+
private Long id;
28+
29+
private String title;
30+
31+
@ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER)
32+
private Set<Student> students = new HashSet<>();
33+
34+
@ElementCollection(fetch = FetchType.EAGER)
35+
private List<String> notes;
36+
37+
public Course() {
38+
}
39+
40+
public Course(String title) {
41+
this(title, Set.of());
42+
}
43+
44+
public Course(String title, Set<Student> students) {
45+
this(title, students, List.of());
46+
}
47+
48+
public Course(String title, Set<Student> students, List<String> notes) {
49+
this.title = title;
50+
this.students = students;
51+
this.notes = notes;
52+
}
53+
54+
public Long getId() {
55+
return id;
56+
}
57+
58+
public void setId(Long id) {
59+
this.id = id;
60+
}
61+
62+
public String getTitle() {
63+
return title;
64+
}
65+
66+
public void setTitle(String title) {
67+
this.title = title;
68+
}
69+
70+
public Set<Student> getStudents() {
71+
return students;
72+
}
73+
74+
public void setStudents(Set<Student> students) {
75+
this.students = students;
76+
}
77+
78+
public List<String> getNotes() {
79+
return notes;
80+
}
81+
82+
public void setNotes(List<String> notes) {
83+
this.notes = notes;
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright and related rights waived via CC0
3+
*
4+
* You should have received a copy of the CC0 legalcode along with this
5+
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
6+
*/
7+
package org_hibernate_reactive.hibernate_reactive_core.entity;
8+
9+
import jakarta.persistence.Entity;
10+
import jakarta.persistence.FetchType;
11+
import jakarta.persistence.GeneratedValue;
12+
import jakarta.persistence.Id;
13+
import jakarta.persistence.JoinColumn;
14+
import jakarta.persistence.JoinTable;
15+
import jakarta.persistence.ManyToMany;
16+
import jakarta.persistence.Table;
17+
18+
import java.util.HashSet;
19+
import java.util.Set;
20+
21+
@Entity
22+
@Table(name = "student")
23+
public class Student {
24+
25+
@Id
26+
@GeneratedValue
27+
private Long id;
28+
29+
private String name;
30+
31+
@ManyToMany(fetch = FetchType.EAGER)
32+
@JoinTable(name = "student_course",
33+
joinColumns = @JoinColumn(name = "student_id"),
34+
inverseJoinColumns = @JoinColumn(name = "course_id")
35+
)
36+
private Set<Course> courses = new HashSet<>();
37+
38+
public Student() {
39+
}
40+
41+
public Student(String name) {
42+
this(name, Set.of());
43+
}
44+
45+
public Student(String name, Set<Course> courses) {
46+
this.name = name;
47+
this.courses = courses;
48+
}
49+
50+
public Long getId() {
51+
return id;
52+
}
53+
54+
public void setId(Long id) {
55+
this.id = id;
56+
}
57+
58+
public String getName() {
59+
return name;
60+
}
61+
62+
public void setName(String name) {
63+
this.name = name;
64+
}
65+
66+
public Set<Course> getCourses() {
67+
return courses;
68+
}
69+
70+
public void setCourses(Set<Course> courses) {
71+
this.courses = courses;
72+
}
73+
74+
}

tests/src/org.hibernate.reactive/hibernate-reactive-core/2.0.0.Final/src/test/resources/META-INF/native-image/hibernate-reactive-core-tests-only/reflect-config.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,72 @@
112112
"name": "<init>",
113113
"parameterTypes": [
114114

115+
]
116+
}
117+
]
118+
},
119+
{
120+
"name": "org_hibernate_reactive.hibernate_reactive_core.entity.Course",
121+
"allDeclaredClasses": true,
122+
"queryAllDeclaredMethods": true,
123+
"allDeclaredFields": true,
124+
"queryAllPublicMethods": true,
125+
"methods": [
126+
{
127+
"name": "<init>",
128+
"parameterTypes": [
129+
130+
]
131+
},
132+
{
133+
"name": "getId",
134+
"parameterTypes": [
135+
136+
]
137+
},
138+
{
139+
"name": "setId",
140+
"parameterTypes": [
141+
"java.lang.Long"
142+
]
143+
},
144+
{
145+
"name": "suite",
146+
"parameterTypes": [
147+
148+
]
149+
}
150+
]
151+
},
152+
{
153+
"name": "org_hibernate_reactive.hibernate_reactive_core.entity.Student",
154+
"allDeclaredClasses": true,
155+
"queryAllDeclaredMethods": true,
156+
"allDeclaredFields": true,
157+
"queryAllPublicMethods": true,
158+
"methods": [
159+
{
160+
"name": "<init>",
161+
"parameterTypes": [
162+
163+
]
164+
},
165+
{
166+
"name": "getId",
167+
"parameterTypes": [
168+
169+
]
170+
},
171+
{
172+
"name": "setId",
173+
"parameterTypes": [
174+
"java.lang.Long"
175+
]
176+
},
177+
{
178+
"name": "suite",
179+
"parameterTypes": [
180+
115181
]
116182
}
117183
]

tests/src/org.hibernate.reactive/hibernate-reactive-core/2.0.0.Final/src/test/resources/META-INF/persistence.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Author</class>
1010
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Book</class>
11+
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Course</class>
12+
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Student</class>
1113

1214
<properties>
1315
<property name="hibernate.connection.pool_size" value="10"/>

0 commit comments

Comments
 (0)