|
| 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 | +} |
0 commit comments