|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.array; |
| 6 | + |
| 7 | +import jakarta.persistence.Embeddable; |
| 8 | +import jakarta.persistence.Entity; |
| 9 | +import jakarta.persistence.Id; |
| 10 | +import jakarta.persistence.Table; |
| 11 | +import org.hibernate.annotations.Struct; |
| 12 | +import org.hibernate.testing.orm.junit.*; |
| 13 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsStructAggregate; |
| 14 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsTypedArrays; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Objects; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 23 | + |
| 24 | +@SessionFactory |
| 25 | +@DomainModel(annotatedClasses = { |
| 26 | + StructArrayWithNullElementTestDemoTest.Book.class, |
| 27 | + StructArrayWithNullElementTestDemoTest.Author.class |
| 28 | +}) |
| 29 | +@RequiresDialectFeature(feature = SupportsStructAggregate.class) |
| 30 | +@RequiresDialectFeature(feature = SupportsTypedArrays.class) |
| 31 | +class StructArrayWithNullElementTestDemoTest { |
| 32 | + |
| 33 | + @Test |
| 34 | + void test(SessionFactoryScope scope) { |
| 35 | + scope.inTransaction( session -> { |
| 36 | + var book = new Book(); |
| 37 | + book.id = 1; |
| 38 | + book.authors = Arrays.asList( |
| 39 | + new Author( "John", "Smith" ), |
| 40 | + null |
| 41 | + ); |
| 42 | + session.persist( book ); |
| 43 | + } ); |
| 44 | + |
| 45 | + scope.inSession( session -> { |
| 46 | + final var book = session.find( Book.class, 1 ); |
| 47 | + assertEquals( 2, book.authors.size() ); |
| 48 | + assertEquals( new Author( "John", "Smith" ), book.authors.get( 0 ) ); |
| 49 | + assertNull( book.authors.get( 1 ) ); |
| 50 | + } ); |
| 51 | + } |
| 52 | + |
| 53 | + @Entity(name = "Book") |
| 54 | + @Table(name = "books") |
| 55 | + static class Book { |
| 56 | + @Id |
| 57 | + int id; |
| 58 | + List<Author> authors; |
| 59 | + } |
| 60 | + |
| 61 | + @Embeddable |
| 62 | + @Struct(name = "Author") |
| 63 | + static final class Author { |
| 64 | + String firstName; |
| 65 | + String lastName; |
| 66 | + |
| 67 | + public Author() { |
| 68 | + } |
| 69 | + |
| 70 | + public Author(String firstName, String lastName) { |
| 71 | + this.firstName = firstName; |
| 72 | + this.lastName = lastName; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean equals(Object o) { |
| 77 | + if (o == null || getClass() != o.getClass()) return false; |
| 78 | + Author author = (Author) o; |
| 79 | + return Objects.equals(firstName, author.firstName) && Objects.equals(lastName, author.lastName); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public int hashCode() { |
| 84 | + return Objects.hash(firstName, lastName); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public String toString() { |
| 89 | + return "Author[firstName=%s, lastName=%s]".formatted(firstName, lastName); |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | +} |
0 commit comments