From 84c4a5709965b711895a5bd986dd0f612a3d283d Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Fri, 9 Sep 2022 13:03:21 +0200 Subject: [PATCH] HHH-15481 Add test for issue --- .../NaturalIdAndLazyLoadingTest.java | 158 ++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/naturalid/NaturalIdAndLazyLoadingTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/naturalid/NaturalIdAndLazyLoadingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/naturalid/NaturalIdAndLazyLoadingTest.java new file mode 100644 index 0000000000..8be153e0be --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/naturalid/NaturalIdAndLazyLoadingTest.java @@ -0,0 +1,158 @@ +/* + * 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.jpa.naturalid; + +import java.math.BigDecimal; +import java.util.List; + +import org.hibernate.Hibernate; +import org.hibernate.annotations.NaturalId; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import jakarta.persistence.Embeddable; +import jakarta.persistence.Embedded; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.Table; +import jakarta.persistence.TypedQuery; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.ParameterExpression; +import jakarta.persistence.criteria.Root; +import jakarta.validation.constraints.NotNull; + +import static org.assertj.core.api.Assertions.assertThat; + + +@Jpa( + annotatedClasses = { + NaturalIdAndLazyLoadingTest.Wallet.class, + NaturalIdAndLazyLoadingTest.Currency.class + } +) +@TestForIssue(jiraKey = "HHH-15481") +public class NaturalIdAndLazyLoadingTest { + + @BeforeEach + public void setUp(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Currency currency = new Currency( 1, "GPB" ); + Wallet position = new Wallet( 1, new BigDecimal( 1 ), currency ); + + entityManager.persist( currency ); + entityManager.persist( position ); + } + ); + } + + @Test + public void testCriteriaQuery(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = criteriaBuilder.createQuery( Wallet.class ); + Root walletRoot = criteriaQuery.from( Wallet.class ); + + ParameterExpression parameter = criteriaBuilder.parameter( Integer.class, "p" ); + + criteriaQuery.where( walletRoot.get( "id" ).in( parameter ) ); + + TypedQuery query = entityManager.createQuery( criteriaQuery ); + query.setParameter( "p", 1 ); + List wallets = query.getResultList(); + + assertThat( wallets.size() ).isEqualTo( 1 ); + + // Currency cannot be lazy initialized without Bytecode Enhancement because of the @JoinColumn(referencedColumnName = "isoCode", nullable = false) + assertThat( Hibernate.isInitialized( wallets.get( 0 ).getCurrency() ) ).isTrue(); + } + ); + } + + @Entity(name = "Wallet") + @Table(name = "WALLET_TABLE") + public static class Wallet { + @Id + private Integer id; + + @Embedded + private Amount amount; + + public Wallet() { + } + + public Wallet(Integer id, BigDecimal quantity, Currency currency) { + this.id = id; + amount = new Amount( quantity, currency ); + } + + public Amount getAmount() { + return amount; + } + + public Currency getCurrency() { + if ( amount == null ) { + return null; + } + return amount.getCurrency(); + } + } + + @Embeddable + public static class Amount { + + private BigDecimal quantity; + + @JoinColumn(referencedColumnName = "isoCode", nullable = false) + @ManyToOne(fetch = FetchType.LAZY) + private Currency currency; + + public Amount() { + } + + public Amount(BigDecimal quantity, Currency currency) { + this.quantity = quantity; + this.currency = currency; + } + + public BigDecimal getQuantity() { + return quantity; + } + + public Currency getCurrency() { + return currency; + } + } + + @Entity(name = "Currency") + @Table(name = "CURRENCY_TABLE") + public static class Currency { + @Id + private Integer id; + + @NaturalId + @NotNull + private String isoCode; + + public Currency() { + } + + public Currency(Integer id, String isoCode) { + this.id = id; + this.isoCode = isoCode; + } + } +}