Skip to content

Commit e657efb

Browse files
committed
HHH-17173 Add test for issue
1 parent 3ef251d commit e657efb

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package org.hibernate.orm.test.bytecode.enhancement.association;
2+
3+
import org.hibernate.Hibernate;
4+
import org.hibernate.cfg.AvailableSettings;
5+
import org.hibernate.cfg.Configuration;
6+
7+
import org.hibernate.testing.bytecode.enhancement.BytecodeEnhancerRunner;
8+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
9+
import org.hibernate.testing.orm.junit.JiraKey;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
14+
import jakarta.persistence.Column;
15+
import jakarta.persistence.Entity;
16+
import jakarta.persistence.GeneratedValue;
17+
import jakarta.persistence.Id;
18+
import jakarta.persistence.OneToOne;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
22+
23+
@JiraKey("HHH-17173")
24+
@RunWith(BytecodeEnhancerRunner.class)
25+
public class OneToOnEnhancedEntityLoadedAsReferenceTest extends BaseCoreFunctionalTestCase {
26+
27+
private long entityId;
28+
private long entityId2;
29+
private long containedEntityId;
30+
31+
@Override
32+
public Class<?>[] getAnnotatedClasses() {
33+
return new Class<?>[] { ContainingEntity.class, ContainedEntity.class };
34+
}
35+
36+
@Override
37+
protected void configure(Configuration configuration) {
38+
configuration.setProperty( AvailableSettings.DEFAULT_BATCH_FETCH_SIZE, "10" );
39+
configuration.setProperty( AvailableSettings.MAX_FETCH_DEPTH, "0" );
40+
}
41+
42+
@Before
43+
public void prepare() {
44+
doInJPA( this::sessionFactory, em -> {
45+
ContainingEntity entity = new ContainingEntity();
46+
ContainedEntity containedEntity = new ContainedEntity();
47+
containedEntity.setValue( "value" );
48+
entity.setContained( containedEntity );
49+
containedEntity.setContaining( entity );
50+
51+
em.persist( entity );
52+
em.persist( containedEntity );
53+
entityId = entity.getId();
54+
containedEntityId = containedEntity.getId();
55+
56+
ContainingEntity entity2 = new ContainingEntity();
57+
em.persist( entity2 );
58+
entityId2 = entity2.getId();
59+
} );
60+
}
61+
62+
@Test
63+
public void test() {
64+
doInJPA( this::sessionFactory, em -> {
65+
ContainingEntity entity2 = em.getReference( ContainingEntity.class, entityId2 );
66+
ContainingEntity entity = em.getReference( ContainingEntity.class, entityId );
67+
ContainedEntity containedEntity = em.getReference( ContainedEntity.class, containedEntityId );
68+
// We're working on an uninitialized proxy.
69+
assertThat( entity ).returns( false, Hibernate::isInitialized );
70+
// The above should have persisted a value that passes the assertion.
71+
assertThat( entity.getContained() ).isEqualTo( containedEntity );
72+
assertThat( entity.getContained().getValue() ).isEqualTo( "value" );
73+
// Accessing the value should trigger initialization of the proxy.
74+
assertThat( entity ).returns( true, Hibernate::isInitialized );
75+
} );
76+
}
77+
78+
@Entity(name = "ContainingEntity")
79+
public static class ContainingEntity {
80+
81+
@Id
82+
@GeneratedValue
83+
public long id;
84+
85+
@OneToOne(mappedBy = "containing")
86+
private ContainedEntity contained;
87+
88+
public long getId() {
89+
return id;
90+
}
91+
92+
public void setId(long id) {
93+
this.id = id;
94+
}
95+
96+
public ContainedEntity getContained() {
97+
return contained;
98+
}
99+
100+
public void setContained(ContainedEntity oneToOneMappedBy) {
101+
this.contained = oneToOneMappedBy;
102+
}
103+
}
104+
105+
@Entity(name = "ContainedEntity")
106+
public static class ContainedEntity {
107+
108+
@Id
109+
@GeneratedValue
110+
private long id;
111+
112+
@Column(name = "value_")
113+
private String value;
114+
115+
@OneToOne
116+
private ContainingEntity containing;
117+
118+
public long getId() {
119+
return id;
120+
}
121+
122+
public void setId(long id) {
123+
this.id = id;
124+
}
125+
126+
public String getValue() {
127+
return value;
128+
}
129+
130+
public void setValue(String value) {
131+
this.value = value;
132+
}
133+
134+
public ContainingEntity getContaining() {
135+
return containing;
136+
}
137+
138+
public void setContaining(ContainingEntity oneToOne) {
139+
this.containing = oneToOne;
140+
}
141+
142+
}
143+
}

0 commit comments

Comments
 (0)