Skip to content

Commit 107e426

Browse files
gtoisonmbellade
authored andcommitted
HHH-18872 Test reproducing the issue
1 parent 1287d93 commit 107e426

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.proxy.concrete;
6+
7+
import jakarta.persistence.Id;
8+
import jakarta.persistence.Cacheable;
9+
import jakarta.persistence.DiscriminatorColumn;
10+
import jakarta.persistence.DiscriminatorType;
11+
import jakarta.persistence.DiscriminatorValue;
12+
import jakarta.persistence.Entity;
13+
import jakarta.persistence.FetchType;
14+
import jakarta.persistence.Inheritance;
15+
import jakarta.persistence.InheritanceType;
16+
import jakarta.persistence.JoinColumn;
17+
import jakarta.persistence.ManyToOne;
18+
import jakarta.persistence.Table;
19+
20+
21+
import org.hibernate.annotations.Cache;
22+
import org.hibernate.annotations.CacheConcurrencyStrategy;
23+
import org.hibernate.annotations.ConcreteProxy;
24+
import org.hibernate.cfg.AvailableSettings;
25+
26+
import org.hibernate.testing.bytecode.enhancement.extension.BytecodeEnhanced;
27+
import org.hibernate.testing.orm.junit.DomainModel;
28+
import org.hibernate.testing.orm.junit.JiraKey;
29+
import org.hibernate.testing.orm.junit.ServiceRegistry;
30+
import org.hibernate.testing.orm.junit.SessionFactory;
31+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
32+
import org.hibernate.testing.orm.junit.Setting;
33+
34+
import static org.junit.jupiter.api.Assertions.assertTrue;
35+
36+
import org.junit.jupiter.api.Test;
37+
38+
/**
39+
* @author Guillaume Toison
40+
*/
41+
@JiraKey( "HHH-18872" )
42+
@DomainModel(
43+
annotatedClasses = {
44+
ConcreteProxyCacheTest.TestNode.class, ConcreteProxyCacheTest.TestCompositeNode.class
45+
}
46+
)
47+
@ServiceRegistry(
48+
settings = {
49+
@Setting( name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true" ),
50+
}
51+
)
52+
@SessionFactory
53+
@BytecodeEnhanced
54+
public class ConcreteProxyCacheTest {
55+
56+
@Test
57+
public void testManyToOne2LcLoadOfConcreteProxy(SessionFactoryScope scope) throws Exception {
58+
scope.inTransaction( s -> {
59+
TestCompositeNode node1 = new TestCompositeNode( 1 );
60+
TestCompositeNode node2 = new TestCompositeNode( 2 );
61+
node1.setParent( node2 );
62+
63+
s.persist( node1 );
64+
s.persist( node2 );
65+
} );
66+
67+
scope.inSession( s -> {
68+
// Test the case when node1 is retrieved from 2LC but node2 is not in cache
69+
// Hibernate would either need to hit the database to retrieve the type or to the store the type in node1's cache entry
70+
s.getSessionFactory().getCache().evict( TestCompositeNode.class, 2 );
71+
72+
TestCompositeNode node1 = s.getReference( TestCompositeNode.class, 1 );
73+
assertTrue( node1.getParent() instanceof TestCompositeNode , "Expecting object to be an instance of TestCompositeNode but the class was " + node1.getParent().getClass() );
74+
} );
75+
76+
}
77+
78+
@Entity
79+
@Table(name = "node")
80+
@Cacheable
81+
@ConcreteProxy
82+
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
83+
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
84+
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "type")
85+
@DiscriminatorValue(value = "SIMPLE")
86+
public static class TestNode {
87+
private Integer id;
88+
89+
public TestNode() {
90+
}
91+
92+
public TestNode(Integer id) {
93+
this.id = id;
94+
}
95+
96+
@Id
97+
public Integer getId() {
98+
return id;
99+
}
100+
101+
public void setId(Integer id) {
102+
this.id = id;
103+
}
104+
}
105+
106+
@Entity
107+
@DiscriminatorValue("COMPOSITE")
108+
public static class TestCompositeNode extends TestNode {
109+
private TestNode parent;
110+
111+
public TestCompositeNode() {
112+
}
113+
114+
public TestCompositeNode(Integer id) {
115+
super(id);
116+
}
117+
118+
@ManyToOne(fetch = FetchType.LAZY)
119+
@JoinColumn(name = "parent_id")
120+
public TestNode getParent() {
121+
return parent;
122+
}
123+
124+
public void setParent(TestNode parent) {
125+
this.parent = parent;
126+
}
127+
}
128+
}

0 commit comments

Comments
 (0)