-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test and reflection metadata for Hibernate Reactive ManyToMany an…
…d ElementCollection (#2919)
- Loading branch information
1 parent
fcd0ea4
commit 43c2c88
Showing
6 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
...META-INF/native-image/io.micronaut.data/data-hibernate-reactive-graal/reflect-config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
] | ||
} | ||
] |
78 changes: 78 additions & 0 deletions
78
doc-examples/hibernate-reactive-example-java/src/main/java/example/Course.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
doc-examples/hibernate-reactive-example-java/src/main/java/example/CourseRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
68 changes: 68 additions & 0 deletions
68
doc-examples/hibernate-reactive-example-java/src/main/java/example/Student.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
doc-examples/hibernate-reactive-example-java/src/main/java/example/StudentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
73 changes: 73 additions & 0 deletions
73
...examples/hibernate-reactive-example-java/src/test/java/example/StudentRepositorySpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |