8000 HHH-13179 Subclass 2nd level caching now works for XML mappings · hibernate/hibernate-orm@8216537 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8216537

Browse files
Lars Karlströmbeikov
authored andcommitted
HHH-13179 Subclass 2nd level caching now works for XML mappings
1 parent 345dc5b commit 8216537

14 files changed

+310
-0
lines changed

hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/ModelBinder.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ private void bindDiscriminatorSubclassEntities(
524524
PersistentClass superEntityDescriptor) {
525525
for ( IdentifiableTypeSource subType : entitySource.getSubTypes() ) {
526526
final SingleTableSubclass subEntityDescriptor = new SingleTableSubclass( superEntityDescriptor, metadataBuildingContext );
527+
subEntityDescriptor.setCached(superEntityDescriptor.isCached());
527528
bindDiscriminatorSubclassEntity( (SubclassEntitySourceImpl) subType, subEntityDescriptor );
528529
superEntityDescriptor.addSubclass( subEntityDescriptor );
529530
entitySource.getLocalMetadataBuildingContext().getMetadataCollector().addEntityBinding( subEntityDescriptor );
@@ -582,6 +583,7 @@ private void bindJoinedSubclassEntities(
582583
PersistentClass superEntityDescriptor) {
583584
for ( IdentifiableTypeSource subType : entitySource.getSubTypes() ) {
584585
final JoinedSubclass subEntityDescriptor = new JoinedSubclass( superEntityDescriptor, metadataBuildingContext );
586+
subEntityDescriptor.setCached(superEntityDescriptor.isCached());
585587
bindJoinedSubclassEntity( (JoinedSubclassEntitySourceImpl) subType, subEntityDescriptor );
586588
superEntityDescriptor.addSubclass( subEntityDescriptor );
587589
entitySource.getLocalMetadataBuildingContext().getMetadataCollector().addEntityBinding( subEntityDescriptor );
@@ -658,6 +660,7 @@ private void bindUnionSubclassEntities(
658660
PersistentClass superEntityDescriptor) {
659661
for ( IdentifiableTypeSource subType : entitySource.getSubTypes() ) {
660662
final UnionSubclass subEntityDescriptor = new UnionSubclass( superEntityDescriptor, metadataBuildingContext );
663+
subEntityDescriptor.setCached(superEntityDescriptor.isCached());
661664
bindUnionSubclassEntity( (SubclassEntitySourceImpl) subType, subEntityDescriptor );
662665
superEntityDescriptor.addSubclass( subEntityDescriptor );
663666
entitySource.getLocalMetadataBuildingContext().getMetadataCollector().addEntityBinding( subEntityDescriptor );
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public class DiscriminatorSubclassNonUIPerson extends DiscriminatorSubclassPerson {
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public abstract class DiscriminatorSubclassPerson {
4+
5+
private Long oid;
6+
7+
public Long getOid() {
8+
return oid;
9+
}
10+
11+
public void setOid(Long oid) {
12+
this.oid = oid;
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public class DiscriminatorSubclassUIPerson extends DiscriminatorSubclassPerson {
4+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
/*
2+
* Copyright 2014 JBoss Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.hibernate.test.cache.hhh13179;
17+
18+
import org.hibernate.Session;
19+
import org.hibernate.Transaction;
20+
import org.hibernate.cfg.AvailableSettings;
21+
import org.hibernate.cfg.Configuration;
22+
import org.hibernate.stat.CacheRegionStatistics;
23+
import org.hibernate.testing.TestForIssue;
24+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
/**
29+
* Check that second level caching works for hbm mapped joined subclass inheritance structures
30+
*/
31+
@TestForIssue( jiraKey = "HHH-13179")
32+
public class HHH13179Test extends BaseCoreFunctionalTestCase {
33+
34+
// Add your entities here.
35+
@Override
36+
protected Class[] getAnnotatedClasses() {
37+
return new Class[] {
38+
JoinedSubclassPerson.class,
39+
JoinedSubclassUIPerson.class,
40+
JoinedSubclassNonUIPerson.class,
41+
UnionSubclassPerson.class,
42+
UnionSubclassUIPerson.class,
43+
UnionSubclassNonUIPerson.class,
44+
DiscriminatorSubclassPerson.class,
45+
DiscriminatorSubclassUIPerson.class,
46+
DiscriminatorSubclassNonUIPerson.class
47+
};
48+
}
49+
50+
// If you use *.hbm.xml mappings, instead of annotations, add the mappings here.
51+
@Override
52+
protected String[] getMappings() {
53+
return new String[] {
54+
"org/hibernate/test/cache/hhh13179/JoinedSubclassPerson.hbm.xml",
55+
"org/hibernate/test/cache/hhh13179/UnionSubclassPerson.hbm.xml",
56+
"org/hibernate/test/cache/hhh13179/DiscriminatorSubclassPerson.hbm.xml"
57+
};
58+
}
59+
// If those mappings reside somewhere other than resources/org/hibernate/test, change this.
60+
@Override
61+
protected String getBaseForMappings() {
62+
return "";
63+
}
64+
65+
// Add in any settings that are specific to your test. See resources/hibernate.properties for the defaults.
66+
@Override
67+
protected void configure(Configuration configuration) {
68+
super.configure( configuration );
69+
70+
configuration.setProperty( AvailableSettings.SHOW_SQL, Boolean.TRUE.toString() );
71+
configuration.setProperty( AvailableSettings.FORMAT_SQL, Boolean.TRUE.toString() );
72+
configuration.setProperty( AvailableSettings.GENERATE_STATISTICS, "true" );
73+
}
74+
75+
@Test
76+
public void testJoinedSubclassCaching() {
77+
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session.
78+
Session s = openSession();
79+
Transaction tx = s.beginTransaction();
80+
81+
String regionName = "org.hibernate.test.cache.hhh13179.JoinedSubclassPerson";
82+
83+
// sanity check
84+
CacheRegionStatistics cacheRegionStatistics = s.getSessionFactory().getStatistics().getCacheRegionStatistics(regionName);
85+
Assert.assertEquals("Cache put should be 0", 0, cacheRegionStatistics.getPutCount());
86+
87+
JoinedSubclassPerson person1 = new JoinedSubclassUIPerson();
88+
person1.setOid(1L);
89+
s.save(person1);
90+
91+
tx.commit();
92+
93+
s.close();
94+
95+
s = openSession();
96+
tx = s.beginTransaction();
97+
98+
JoinedSubclassPerson person2 = s.get(JoinedSubclassPerson.class, 1L);
99+
100+
cacheRegionStatistics = s.getSessionFactory().getStatistics().getCacheRegionStatistics(regionName);
101+
Assert.assertEquals("Cache hit should be 1", 1, cacheRegionStatistics.getHitCount());
102+
Assert.assertEquals("Cache put should be 1", 1, cacheRegionStatistics.getPutCount());
103+
104+
tx.commit();
105+
s.close();
106+
}
107+
108+
@Test
109+
public void testUnionSubclassCaching() {
110+
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session.
111+
Session s = openSession();
112+
Transaction tx = s.beginTransaction();
113+
114+
String regionName = "org.hibernate.test.cache.hhh13179.UnionSubclassPerson";
115+
116+
// sanity check
117+
CacheRegionStatistics cacheRegionStatistics = s.getSessionFactory().getStatistics().getCacheRegionStatistics(regionName);
118+
Assert.assertEquals("Cache put should be 0", 0, cacheRegionStatistics.getPutCount());
119+
120+
UnionSubclassPerson person1 = new UnionSubclassUIPerson();
121+
person1.setOid(1L);
122+
s.save(person1);
123+
124+
tx.commit();
125+
126+
s.close();
127+
128+
s = openSession();
129+
tx = s.beginTransaction();
130+
131+
UnionSubclassPerson person2 = s.get(UnionSubclassPerson.class, 1L);
132+
133+
cacheRegionStatistics = s.getSessionFactory().getStatistics().getCacheRegionStatistics(regionName);
134+
Assert.assertEquals("Cache hit should be 1", 1, cacheRegionStatistics.getHitCount());
135+
Assert.assertEquals("Cache put should be 1", 1, cacheRegionStatistics.getPutCount());
136+
137+
tx.commit();
138+
s.close();
139+
}
140+
141< F438 code class="diff-text syntax-highlighted-line addition">+
@Test
142+
public void testDiscriminatorSubclassCaching() {
143+
// BaseCoreFunctionalTestCase automatically creates the SessionFactory and provides the Session.
144+
Session s = openSession();
145+
Transaction tx = s.beginTransaction();
146+
147+
String regionName = "org.hibernate.test.cache.hhh13179.DiscriminatorSubclassPerson";
148+
149+
// sanity check
150+
CacheRegionStatistics cacheRegionStatistics = s.getSessionFactory().getStatistics().getCacheRegionStatistics(regionName);
151+
Assert.assertEquals("Cache put should be 0", 0, cacheRegionStatistics.getPutCount());
152+
153+
DiscriminatorSubclassPerson person1 = new DiscriminatorSubclassUIPerson();
154+
person1.setOid(1L);
155+
s.save(person1);
156+
157+
tx.commit();
158+
159+
s.close();
160+
161+
s = openSession();
162+
tx = s.beginTransaction();
163+
164+
DiscriminatorSubclassPerson person2 = s.get(DiscriminatorSubclassPerson.class, 1L);
165+
166+
cacheRegionStatistics = s.getSessionFactory().getStatistics().getCacheRegionStatistics(regionName);
167+
Assert.assertEquals("Cache hit should be 1", 1, cacheRegionStatistics.getHitCount());
168+
Assert.assertEquals("Cache put should be 1", 1, cacheRegionStatistics.getPutCount());
169+
170+
tx.commit();
171+
s.close();
172+
}
173+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public class JoinedSubclassNonUIPerson extends JoinedSubclassPerson {
4+
}
Lines changed: 14 additions & 0 del 10000 etions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public abstract class JoinedSubclassPerson {
4+
5+
private Long oid;
6+
7+
public Long getOid() {
8+
return oid;
9+
}
10+
11+
public void setOid(Long oid) {
12+
this.oid = oid;
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public class JoinedSubclassUIPerson extends JoinedSubclassPerson {
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public class UnionSubclassNonUIPerson extends UnionSubclassPerson {
4+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.hibernate.test.cache.hhh13179;
2+
3+
public abstract class UnionSubclassPerson {
4+
5+
private Long oid;
6+
7+
public Long getOid() {
8+
return oid;
9+
}
10+
11+
public void setOid(Long oid) {
12+
this.oid = oid;
13+
}
14+
}

0 commit comments

Comments
 (0)
0