Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing metadata for Hibernate Reactive (needed for some collections mappings) #489

Merged
merged 3 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,22 @@
"condition": {
"typeReachable": "org.hibernate.reactive.pool.impl.DefaultSqlClientPool"
}
},
{
"name": "org.hibernate.reactive.persister.collection.impl.ReactiveBasicCollectionPersister",
"queryAllPublicMethods": true,
"condition": {
"typeReachable": "org.hibernate.reactive.session.impl.ReactiveSessionFactoryImpl"
},
"methods": [
{
"name": "<init>",
"parameterTypes": [
"org.hibernate.mapping.Collection",
"org.hibernate.cache.spi.access.CollectionDataAccess",
"org.hibernate.metamodel.spi.RuntimeModelCreationContext"
]
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import org.slf4j.LoggerFactory;
import org_hibernate_reactive.hibernate_reactive_core.entity.Author;
import org_hibernate_reactive.hibernate_reactive_core.entity.Book;
import org_hibernate_reactive.hibernate_reactive_core.entity.Course;
import org_hibernate_reactive.hibernate_reactive_core.entity.Student;

import java.io.IOException;
import java.sql.Connection;
Expand All @@ -26,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.Callable;

import static java.time.Month.JANUARY;
Expand Down Expand Up @@ -158,4 +161,62 @@ void testSaveAndLoad() {
Tuple.tuple(book1.getTitle(), author1.getName()),
Tuple.tuple(book2.getTitle(), author2.getName()));
}

/**
* Tests @{@link jakarta.persistence.ManyToMany} and @{@link jakarta.persistence.ElementCollection} relations with Hibernate Reactive.
*/
@Test
void testCollections() {
final Course languageCourse = new Course("English");
languageCourse.setNotes(List.of("Starting in December"));
final Course mathCourse = new Course("Mathematics");

// Save courses
factory.withTransaction((session, tx) -> session.persist(languageCourse, mathCourse))
.toCompletableFuture()
.join();

// retrieve a Course
Course loadedLanguageCourse = factory.withSession(session -> session.find(Course.class, languageCourse.getId()))
.toCompletableFuture()
.join();
assertThat(loadedLanguageCourse).isNotNull();
assertThat(loadedLanguageCourse.getStudents().size()).isEqualTo(0);
assertThat(loadedLanguageCourse.getNotes().size()).isEqualTo(1);

Course loadedMathCourse = factory.withSession(session -> session.find(Course.class, mathCourse.getId()))
.toCompletableFuture()
.join();
assertThat(loadedMathCourse).isNotNull();
assertThat(loadedMathCourse.getStudents().size()).isEqualTo(0);
assertThat(loadedMathCourse.getNotes().size()).isEqualTo(0);

final Student student = new Student("Peter", Set.of(mathCourse));
// Save Student
factory.withTransaction((session, tx) -> session.persist(student))
.toCompletableFuture()
.join();
Long id = student.getId();
assertThat(id).isNotNull();

// retrieve a Student and verify that student has course assigned
Student loadedStudent = factory.withSession(session -> session.find(Student.class, student.getId()))
.toCompletableFuture()
.join();
assertThat(loadedStudent).isNotNull();
assertThat(loadedStudent.getName()).isEqualTo("Peter");
assertThat(loadedStudent.getCourses().size()).isEqualTo(1);

loadedLanguageCourse = factory.withSession(session -> session.find(Course.class, languageCourse.getId()))
.toCompletableFuture()
.join();
assertThat(loadedLanguageCourse).isNotNull();
assertThat(loadedLanguageCourse.getStudents().size()).isEqualTo(0);

loadedMathCourse = factory.withSession(session -> session.find(Course.class, mathCourse.getId()))
.toCompletableFuture()
.join();
assertThat(loadedMathCourse).isNotNull();
assertThat(loadedMathCourse.getStudents().size()).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package org_hibernate_reactive.hibernate_reactive_core.entity;

import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "course")
public class Course {

@Id
@GeneratedValue
private Long id;

private String title;

@ManyToMany(mappedBy = "courses", fetch = FetchType.EAGER)
private Set<Student> students = new HashSet<>();

@ElementCollection(fetch = FetchType.EAGER)
private List<String> notes;

public Course() {
}

public Course(String title) {
this(title, Set.of());
}

public Course(String title, Set<Student> students) {
this(title, students, List.of());
}

public Course(String title, Set<Student> students, List<String> notes) {
this.title = title;
this.students = students;
this.notes = notes;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public Set<Student> getStudents() {
return students;
}

public void setStudents(Set<Student> students) {
this.students = students;
}

public List<String> getNotes() {
return notes;
}

public void setNotes(List<String> notes) {
this.notes = notes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package org_hibernate_reactive.hibernate_reactive_core.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.Table;

import java.util.HashSet;
import java.util.Set;

@Entity
@Table(name = "student")
public class Student {

@Id
@GeneratedValue
private Long id;

private String name;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "student_course",
joinColumns = @JoinColumn(name = "student_id"),
inverseJoinColumns = @JoinColumn(name = "course_id")
)
private Set<Course> courses = new HashSet<>();

public Student() {
}

public Student(String name) {
this(name, Set.of());
}

public Student(String name, Set<Course> courses) {
this.name = name;
this.courses = courses;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Set<Course> getCourses() {
return courses;
}

public void setCourses(Set<Course> courses) {
this.courses = courses;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,72 @@
"name": "<init>",
"parameterTypes": [

]
}
]
},
{
"name": "org_hibernate_reactive.hibernate_reactive_core.entity.Course",
"allDeclaredClasses": true,
"queryAllDeclaredMethods": true,
"allDeclaredFields": true,
"queryAllPublicMethods": true,
"methods": [
{
"name": "<init>",
"parameterTypes": [

]
},
{
"name": "getId",
"parameterTypes": [

]
},
{
"name": "setId",
"parameterTypes": [
"java.lang.Long"
]
},
{
"name": "suite",
"parameterTypes": [

]
}
]
},
{
"name": "org_hibernate_reactive.hibernate_reactive_core.entity.Student",
"allDeclaredClasses": true,
"queryAllDeclaredMethods": true,
"allDeclaredFields": true,
"queryAllPublicMethods": true,
"methods": [
{
"name": "<init>",
"parameterTypes": [

]
},
{
"name": "getId",
"parameterTypes": [

]
},
{
"name": "setId",
"parameterTypes": [
"java.lang.Long"
]
},
{
"name": "suite",
"parameterTypes": [

]
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

<class>org_hibernate_reactive.hibernate_reactive_core.entity.Author</class>
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Book</class>
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Course</class>
<class>org_hibernate_reactive.hibernate_reactive_core.entity.Student</class>

<properties>
<property name="hibernate.connection.pool_size" value="10"/>
Expand Down
Loading