Skip to content

Commit 84c4a57

Browse files
committed
HHH-15481 Add test for issue
1 parent 76c67f0 commit 84c4a57

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.orm.test.jpa.naturalid;
8+
9+
import java.math.BigDecimal;
10+
import java.util.List;
11+
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.annotations.NaturalId;
14+
15+
import org.hibernate.testing.TestForIssue;
16+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
17+
import org.hibernate.testing.orm.junit.Jpa;
18+
import org.junit.jupiter.api.BeforeEach;
19+
import org.junit.jupiter.api.Test;
20+
21+
import jakarta.persistence.Embeddable;
22+
import jakarta.persistence.Embedded;
23+
import jakarta.persistence.Entity;
24+
import jakarta.persistence.FetchType;
25+
import jakarta.persistence.Id;
26+
import jakarta.persistence.JoinColumn;
27+
import jakarta.persistence.ManyToOne;
28+
import jakarta.persistence.Table;
29+
import jakarta.persistence.TypedQuery;
30+
import jakarta.persistence.criteria.CriteriaBuilder;
31+
import jakarta.persistence.criteria.CriteriaQuery;
32+
import jakarta.persistence.criteria.ParameterExpression;
33+
import jakarta.persistence.criteria.Root;
34+
import jakarta.validation.constraints.NotNull;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
39+
@Jpa(
40+
annotatedClasses = {
41+
NaturalIdAndLazyLoadingTest.Wallet.class,
42+
NaturalIdAndLazyLoadingTest.Currency.class
43+
}
44+
)
45+
@TestForIssue(jiraKey = "HHH-15481")
46+
public class NaturalIdAndLazyLoadingTest {
47+
48+
@BeforeEach
49+
public void setUp(EntityManagerFactoryScope scope) {
50+
scope.inTransaction(
51+
entityManager -> {
52+
Currency currency = new Currency( 1, "GPB" );
53+
Wallet position = new Wallet( 1, new BigDecimal( 1 ), currency );
54+
55+
entityManager.persist( currency );
56+
entityManager.persist( position );
57+
}
58+
);
59+
}
60+
61+
@Test
62+
public void testCriteriaQuery(EntityManagerFactoryScope scope) {
63+
scope.inTransaction(
64+
entityManager -> {
65+
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
66+
CriteriaQuery<Wallet> criteriaQuery = criteriaBuilder.createQuery( Wallet.class );
67+
Root<Wallet> walletRoot = criteriaQuery.from( Wallet.class );
68+
69+
ParameterExpression<Integer> parameter = criteriaBuilder.parameter( Integer.class, "p" );
70+
71+
criteriaQuery.where( walletRoot.get( "id" ).in( parameter ) );
72+
73+
TypedQuery<Wallet> query = entityManager.createQuery( criteriaQuery );
74+
query.setParameter( "p", 1 );
75+
List<Wallet> wallets = query.getResultList();
76+
77+
assertThat( wallets.size() ).isEqualTo( 1 );
78+
79+
// Currency cannot be lazy initialized without Bytecode Enhancement because of the @JoinColumn(referencedColumnName = "isoCode", nullable = false)
80+
assertThat( Hibernate.isInitialized( wallets.get( 0 ).getCurrency() ) ).isTrue();
81+
}
82+
);
83+
}
84+
85+
@Entity(name = "Wallet")
86+
@Table(name = "WALLET_TABLE")
87+
public static class Wallet {
88+
@Id
89+
private Integer id;
90+
91+
@Embedded
92+
private Amount amount;
93+
94+
public Wallet() {
95+
}
96+
97+
public Wallet(Integer id, BigDecimal quantity, Currency currency) {
98+
this.id = id;
99+
amount = new Amount( quantity, currency );
100+
}
101+
102+
public Amount getAmount() {
103+
return amount;
104+
}
105+
106+
public Currency getCurrency() {
107+
if ( amount == null ) {
108+
return null;
109+
}
110+
return amount.getCurrency();
111+
}
112+
}
113+
114+
@Embeddable
115+
public static class Amount {
116+
117+
private BigDecimal quantity;
118+
119+
@JoinColumn(referencedColumnName = "isoCode", nullable = false)
120+
@ManyToOne(fetch = FetchType.LAZY)
121+
private Currency currency;
122+
123+
public Amount() {
124+
}
125+
126+
public Amount(BigDecimal quantity, Currency currency) {
127+
this.quantity = quantity;
128+
this.currency = currency;
129+
}
130+
131+
public BigDecimal getQuantity() {
132+
return quantity;
133+
}
134+
135+
public Currency getCurrency() {
136+
return currency;
137+
}
138+
}
139+
140+
@Entity(name = "Currency")
141+
@Table(name = "CURRENCY_TABLE")
142+
public static class Currency {
143+
@Id
144+
private Integer id;
145+
146+
@NaturalId
147+
@NotNull
148+
private String isoCode;
149+
150+
public Currency() {
151+
}
152+
153+
public Currency(Integer id, String isoCode) {
154+
this.id = id;
155+
this.isoCode = isoCode;
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)