|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.mapping.access; |
| 6 | + |
| 7 | +import jakarta.persistence.CascadeType; |
| 8 | +import jakarta.persistence.ElementCollection; |
| 9 | +import jakarta.persistence.Embeddable; |
| 10 | +import jakarta.persistence.Entity; |
| 11 | +import jakarta.persistence.GeneratedValue; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.OneToMany; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.AfterAll; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 26 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 27 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 28 | + |
| 29 | +@JiraKey(value = "HHH-13721") |
| 30 | +@DomainModel( |
| 31 | + annotatedClasses = { |
| 32 | + ElementCollectionInOneToManyTest.Author.class, ElementCollectionInOneToManyTest.Book.class |
| 33 | + } |
| 34 | +) |
| 35 | +@SessionFactory |
| 36 | +public class ElementCollectionInOneToManyTest { |
| 37 | + |
| 38 | + @Test |
| 39 | + void hhh13721Test(SessionFactoryScope scope) { |
| 40 | + |
| 41 | + // prepare data |
| 42 | + scope.inTransaction( session -> { |
| 43 | + |
| 44 | + Chapter c1 = new Chapter(); |
| 45 | + c1.name = "chapter1"; |
| 46 | + |
| 47 | + Chapter c2 = new Chapter(); |
| 48 | + c2.name = "chapter2"; |
| 49 | + |
| 50 | + Chapter c3 = new Chapter(); |
| 51 | + c3.name = "chapter3"; |
| 52 | + |
| 53 | + Book book = new Book(); |
| 54 | + book.name = "book"; |
| 55 | + book.chapters.add( c1 ); |
| 56 | + book.chapters.add( c2 ); |
| 57 | + book.chapters.add( c3 ); |
| 58 | + |
| 59 | + Author author = new Author(); |
| 60 | + author.name = "author"; |
| 61 | + author.books.add( book ); |
| 62 | + |
| 63 | + session.persist( author ); |
| 64 | + } ); |
| 65 | + |
| 66 | + // find and assert |
| 67 | + scope.inTransaction( session -> { |
| 68 | + |
| 69 | + Author author = session.createSelectionQuery( "select a from Author a", Author.class ).getSingleResult(); |
| 70 | + assertNotNull( author ); |
| 71 | + assertEquals( "author", author.name ); |
| 72 | + assertEquals( 1, author.books.size() ); |
| 73 | + |
| 74 | + Book book = author.books.getFirst(); |
| 75 | + assertEquals( "book", book.name ); |
| 76 | + assertEquals( 3, book.chapters.size() ); |
| 77 | + |
| 78 | + List<String> chapters = book.chapters.stream().map( c -> c.name ).toList(); |
| 79 | + assertTrue( chapters.contains( "chapter1" ) ); |
| 80 | + assertTrue( chapters.contains( "chapter2" ) ); |
| 81 | + assertTrue( chapters.contains( "chapter3" ) ); |
| 82 | + } ); |
| 83 | + } |
| 84 | + @AfterAll |
| 85 | + public void tearDown(SessionFactoryScope scope) { |
| 86 | + scope.getSessionFactory().getSchemaManager().truncateMappedObjects(); |
| 87 | + } |
| 88 | + @Entity(name = "Author") |
| 89 | + public static class Author { |
| 90 | + |
| 91 | + @Id |
| 92 | + @GeneratedValue |
| 93 | + long id; |
| 94 | + |
| 95 | + String name; |
| 96 | + |
| 97 | + @OneToMany(cascade = CascadeType.ALL) |
| 98 | + List<Book> books = new ArrayList<>(); |
| 99 | + } |
| 100 | + |
| 101 | + @Entity(name = "Book") |
| 102 | + public static class Book { |
| 103 | + |
| 104 | + @Id |
| 105 | + @GeneratedValue |
| 106 | + long id; |
| 107 | + |
| 108 | + String name; |
| 109 | + |
| 110 | + @ElementCollection |
| 111 | + Collection<Chapter> chapters = new ArrayList<>(); |
| 112 | + } |
| 113 | + |
| 114 | + @Embeddable |
| 115 | + public static class Chapter { |
| 116 | + |
| 117 | + String name; |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments