Skip to content

Commit

Permalink
Add test and reflection metadata for Hibernate Reactive ManyToMany an…
Browse files Browse the repository at this point in the history
…d ElementCollection (#2919)
  • Loading branch information
radovanradic authored Apr 26, 2024
1 parent fcd0ea4 commit 43c2c88
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{
"name": "org.hibernate.reactive.persister.collection.impl.ReactiveBasicCollectionPersister",
"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
@@ -0,0 +1,78 @@
package example;

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,16 @@
package example;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.annotation.Join;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.reactive.ReactorCrudRepository;
import reactor.core.publisher.Mono;

@Repository
public interface CourseRepository extends ReactorCrudRepository<Course, Long> {

@NonNull
@Override
@Join(value = "students", type = Join.Type.LEFT)
Mono<Course> findById(@NonNull Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package example;

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
@@ -0,0 +1,16 @@
package example;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.data.annotation.Join;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.repository.reactive.ReactorCrudRepository;
import reactor.core.publisher.Mono;

@Repository
public interface StudentRepository extends ReactorCrudRepository<Student, Long> {

@NonNull
@Override
@Join(value = "courses", type = Join.Type.LEFT)
Mono<Student> findById(@NonNull Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package example;

import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

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

import static org.junit.jupiter.api.Assertions.*;

@MicronautTest(transactional = false)
class StudentRepositorySpec {

@Inject
StudentRepository studentRepository;

@Inject
CourseRepository courseRepository;

@AfterEach
public void cleanup() {
studentRepository.deleteAll().block();
}

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

languageCourse = courseRepository.findById(languageCourse.getId()).block();
assertNotNull(languageCourse);
assertEquals(0, languageCourse.getStudents().size());
assertEquals(1, languageCourse.getNotes().size());

mathCourse = courseRepository.findById(mathCourse.getId()).block();
assertNotNull(mathCourse);
assertEquals(0, mathCourse.getStudents().size());
assertEquals(0, mathCourse.getNotes().size());

Student student = new Student("Peter", Set.of(mathCourse));
studentRepository.save(student).block();
Long id = student.getId();
assertNotNull(id);

student = studentRepository.findById(id).block();
assertNotNull(student);
assertEquals("Peter", student.getName());
assertEquals(1, student.getCourses().size());

assertEquals(1, studentRepository.count().block());
assertTrue(studentRepository.findAll().toIterable().iterator().hasNext());

languageCourse = courseRepository.findById(languageCourse.getId()).block();
assertNotNull(languageCourse);
assertEquals(0, languageCourse.getStudents().size());

mathCourse = courseRepository.findById(mathCourse.getId()).block();
assertNotNull(mathCourse);
assertEquals(1, mathCourse.getStudents().size());

studentRepository.deleteById(id).block();
assertEquals(0, studentRepository.count().block());
}

}

0 comments on commit 43c2c88

Please sign in to comment.