|
| 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.DialectFeatureChecks.SupportsStructAggregate; |
| 13 | +import org.hibernate.testing.orm.junit.DialectFeatureChecks.SupportsTypedArrays; |
| 14 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 15 | +import org.hibernate.testing.orm.junit.RequiresDialectFeature; |
| 16 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 17 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 25 | + |
| 26 | +@SessionFactory |
| 27 | +@DomainModel(annotatedClasses = { |
| 28 | + StructArrayWithNullElementTestDemoTest.Book.class, |
| 29 | + StructArrayWithNullElementTestDemoTest.Author.class |
| 30 | +}) |
| 31 | +@RequiresDialectFeature(feature = SupportsStructAggregate.class) |
| 32 | +@RequiresDialectFeature(feature = SupportsTypedArrays.class) |
| 33 | +class StructArrayWithNullElementTestDemoTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + void test(SessionFactoryScope scope) { |
| 37 | + scope.inTransaction( session -> { |
| 38 | + var book = new Book(); |
| 39 | + book.id = 1; |
| 40 | + book.authors = Arrays.asList( |
| 41 | + new Author( "John", "Smith" ), |
| 42 | + null |
| 43 | + ); |
| 44 | + session.persist( book ); |
| 45 | + } ); |
| 46 | + |
| 47 | + scope.inSession( session -> { |
| 48 | + final var book = session.find( Book.class, 1 ); |
| 49 | + assertEquals( 2, book.authors.size() ); |
| 50 | + assertEquals( new Author( "John", "Smith" ), book.authors.get( 0 ) ); |
| 51 | + assertNull( book.authors.get( 1 ) ); |
| 52 | + } ); |
| 53 | + } |
| 54 | + |
| 55 | + @Entity(name = "Book") |
| 56 | + @Table(name = "books") |
| 57 | + static class Book { |
| 58 | + @Id |
| 59 | + int id; |
| 60 | + List<Author> authors; |
| 61 | + } |
| 62 | + |
| 63 | + @Embeddable |
| 64 | + @Struct(name = "Author") |
| 65 | + record Author(String firstName, String lastName) { |
| 66 | + } |
| 67 | +} |
0 commit comments