From 70638f9e60b853438329670abec87b324d71b933 Mon Sep 17 00:00:00 2001 From: Georg Echterling Date: Tue, 29 Nov 2022 13:19:25 +0100 Subject: [PATCH] HHH-15778 Add test for Embeddable with FetchTiming.DELAYED --- .../query/sql/EmbeddableLazyFetchTest.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/EmbeddableLazyFetchTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/EmbeddableLazyFetchTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/EmbeddableLazyFetchTest.java new file mode 100644 index 0000000000..898a8cae36 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/query/sql/EmbeddableLazyFetchTest.java @@ -0,0 +1,108 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.orm.test.query.sql; + +import java.util.Collections; + +import org.hibernate.graph.spi.RootGraphImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DomainModel; +import org.hibernate.testing.orm.junit.SessionFactory; +import org.hibernate.testing.orm.junit.SessionFactoryScope; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Embeddable; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@DomainModel( + annotatedClasses = EmbeddableLazyFetchTest.Person.class +) +@SessionFactory +@TestForIssue(jiraKey = "HHH-15778") +public class EmbeddableLazyFetchTest { + + @BeforeEach + public void setUp(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + Address address = new Address( "Milan", "Italy", "Italy", "20133" ); + Person person = new Person( 1, address ); + + session.persist( person ); + } + ); + } + + @AfterEach + public void tearDown(SessionFactoryScope scope) { + scope.inTransaction( + session -> + session.createMutationQuery( "delete from Person" ).executeUpdate() + ); + } + + @Test + public void testSelect(SessionFactoryScope scope) { + scope.inTransaction( + session -> { + RootGraphImplementor graph = session.createEntityGraph( Person.class ); + + Person person = session.find( + Person.class, 1, + Collections.singletonMap( "jakarta.persistence.fetchgraph", graph ) + ); + + assertThat( person ).isNotNull(); + assertThat( person.address.city ).isEqualTo( "Milan" ); + assertThat( person.address.state ).isEqualTo( "Italy" ); + assertThat( person.address.country ).isEqualTo( "Italy" ); + assertThat( person.address.postcode ).isEqualTo( "20133" ); + } + ); + } + + @Entity(name = "Person") + public static class Person { + @Id + private Integer id; + + private Address address; + + public Person() { + } + + public Person(Integer id, Address address) { + this.id = id; + this.address = address; + } + } + + @Embeddable + public static class Address { + private String city; + private String state; + private String country; + + private String postcode; + + public Address() { + } + + public Address(String city, String state, String country, String postcode) { + this.city = city; + this.state = state; + this.country = country; + this.postcode = postcode; + } + } +}