Skip to content

Commit aba0c6f

Browse files
dreab8beikov
authored andcommitted
HHH-18147 Add test for issue
1 parent 9139a52 commit aba0c6f

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package org.hibernate.orm.test.query.naturalid;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.hibernate.annotations.NaturalId;
7+
8+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
9+
import org.hibernate.testing.orm.junit.JiraKey;
10+
import org.hibernate.testing.orm.junit.Jpa;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import jakarta.persistence.Column;
15+
import jakarta.persistence.ElementCollection;
16+
import jakarta.persistence.Embeddable;
17+
import jakarta.persistence.Entity;
18+
import jakarta.persistence.FetchType;
19+
import jakarta.persistence.Id;
20+
import jakarta.persistence.JoinColumn;
21+
import jakarta.persistence.JoinTable;
22+
import jakarta.persistence.ManyToOne;
23+
import jakarta.persistence.Table;
24+
25+
@Jpa(
26+
annotatedClasses = {
27+
PersistEntityWithProxyAssociationTest.Currency.class,
28+
PersistEntityWithProxyAssociationTest.Position.class,
29+
}
30+
)
31+
@JiraKey("HHH-18147")
32+
public class PersistEntityWithProxyAssociationTest {
33+
34+
public static final long CURRENCY_ID = 1l;
35+
36+
@BeforeEach
37+
public void setUp(EntityManagerFactoryScope scope) {
38+
scope.inTransaction(
39+
entityManager ->
40+
entityManager.persist( new Currency( CURRENCY_ID, "USD" ) )
41+
);
42+
}
43+
44+
@Test
45+
public void testPersistDoesNotThrowConstraintViolationException(EntityManagerFactoryScope scope) {
46+
scope.inTransaction(
47+
entityManager -> {
48+
// using getReference to obtain a Proxy
49+
Currency currency = entityManager.getReference( Currency.class, CURRENCY_ID );
50+
List<Holding> holdings = new ArrayList<>();
51+
holdings.add( new Holding( 20, currency ) );
52+
53+
// Position#currency proxy has to be initialized in order to get its code
54+
// and insert it into POSITION_HOLDING table
55+
entityManager.persist( new Position( 10l, holdings ) );
56+
}
57+
);
58+
}
59+
60+
@Entity
61+
@Table(name = "CURRENCY_TABLE")
62+
public static class Currency {
63+
@Id
64+
private Long id;
65+
66+
@NaturalId
67+
private String code;
68+
69+
public Currency() {
70+
}
71+
72+
public Currency(Long id, String code) {
73+
this.id = id;
74+
this.code = code;
75+
}
76+
77+
public String getCode() {
78+
return code;
79+
}
80+
}
81+
82+
83+
@Entity
84+
@Table(name = "POSITION_TABLE")
85+
public static class Position {
86+
@Id
87+
private Long id;
88+
89+
private boolean active;
90+
91+
@ElementCollection
92+
@JoinTable(name = "POSITION_HOLDING", joinColumns = { @JoinColumn(name = "POSITION_ID") })
93+
private List<Holding> holdings = new ArrayList<>();
94+
95+
public Position() {
96+
}
97+
98+
public Position(Long id) {
99+
this.id = id;
100+
}
101+
102+
public Position(Long id, List<Holding> holdings) {
103+
this.id = id;
104+
this.holdings = holdings;
105+
}
106+
107+
public Long getId() {
108+
return id;
109+
}
110+
111+
public List<Holding> getHoldings() {
112+
return holdings;
113+
}
114+
115+
public void addHolding(Holding holding) {
116+
holdings.add( holding );
117+
}
118+
}
119+
120+
@Embeddable
121+
public static class Holding {
122+
@Column(nullable = false)
123+
private Integer quantity;
124+
125+
@ManyToOne(optional = false, fetch = FetchType.LAZY)
126+
@JoinColumn(referencedColumnName = "code", nullable = false)
127+
private Currency currency;
128+
129+
public Holding() {
130+
}
131+
132+
public Holding(Integer quantity, Currency currency) {
133+
this.quantity = quantity;
134+
this.currency = currency;
135+
}
136+
137+
public Integer getQuantity() {
138+
return quantity;
139+
}
140+
141+
public Currency getCurrency() {
142+
return currency;
143+
}
144+
}
145+
146+
}

0 commit comments

Comments
 (0)