Skip to content

Commit 5280a47

Browse files
David EllingsworthDavid Ellingsworth
authored andcommitted
HHH-14216 Add one-to-one second level cache tests.
1 parent 8d4de09 commit 5280a47

File tree

9 files changed

+300
-0
lines changed

9 files changed

+300
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
6+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
7+
-->
8+
<!DOCTYPE hibernate-mapping PUBLIC
9+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
10+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
11+
12+
13+
<hibernate-mapping package="org.hibernate.test.onetoone.cache">
14+
<class name="DetailsByFK">
15+
<cache usage="read-write"/>
16+
<id name="id" unsaved-value="0">
17+
<generator class="foreign">
18+
<param name="property">person</param>
19+
</generator>
20+
</id>
21+
<one-to-one name="person" class="PersonByFK" constrained="true"/>
22+
<property name="data"/>
23+
</class>
24+
25+
<class name="DetailsByRef">
26+
<cache usage="read-write"/>
27+
<id name="id" unsaved-value="0">
28+
<generator class="native"/>
29+
</id>
30+
<many-to-one name="person" class="PersonByRef" column="personId" unique="true"/>
31+
<property name="data"/>
32+
</class>
33+
</hibernate-mapping>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.onetoone.cache;
8+
import java.io.Serializable;
9+
10+
public abstract class Details implements Serializable {
11+
private int id;
12+
private String data;
13+
private Person person;
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getData() {
24+
return data;
25+
}
26+
27+
public void setData(String data) {
28+
this.data = data;
29+
}
30+
31+
public Person getPerson() {
32+
return person;
33+
}
34+
35+
protected void setPerson(Person person) {
36+
this.person = person;
37+
}
38+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.onetoone.cache;
8+
9+
public class DetailsByFK extends Details {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.onetoone.cache;
8+
9+
public class DetailsByRef extends Details {}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package org.hibernate.test.onetoone.cache;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.Serializable;
6+
import java.lang.reflect.Constructor;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import org.hibernate.Session;
11+
import org.hibernate.Transaction;
12+
import org.hibernate.cache.spi.CacheImplementor;
13+
import org.hibernate.cfg.AvailableSettings;
14+
import org.hibernate.cfg.Configuration;
15+
import org.hibernate.engine.spi.SessionFactoryImplementor;
16+
import org.hibernate.stat.spi.StatisticsImplementor;
17+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
18+
import org.junit.Test;
19+
20+
public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
21+
@Override
22+
public String[] getMappings() {
23+
return new String[] {
24+
"onetoone/cache/Details.hbm.xml",
25+
"onetoone/cache/Person.hbm.xml",
26+
};
27+
}
28+
29+
@Override
30+
protected void configure(Configuration configuration) {
31+
configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
32+
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
33+
}
34+
35+
private <TPerson extends Person, TDetails extends Details> void OneToOneTest(Class<TPerson> personClass,
36+
Class<TDetails> detailsClass) throws Exception {
37+
38+
// Initialize the database with data.
39+
List<Serializable> ids = createPersonsAndDetails(personClass, detailsClass);
40+
41+
// Clear the second level cache and the statistics.
42+
SessionFactoryImplementor sfi = sessionFactory();
43+
CacheImplementor cache = sfi.getCache();
44+
StatisticsImplementor statistics = sfi.getStatistics();
45+
46+
cache.evictEntityData(personClass);
47+
cache.evictEntityData(detailsClass);
48+
cache.evictQueryRegions();
49+
50+
statistics.clear();
51+
52+
// Fill the empty caches with data.
53+
this.getPersons(personClass, ids);
54+
55+
// Verify that no data was retrieved from the cache.
56+
assertEquals("Second level cache hit count", 0, statistics.getSecondLevelCacheHitCount());
57+
58+
statistics.clear();
59+
60+
this.getPersons(personClass, ids);
61+
62+
// Verify that all data was retrieved from the cache.
63+
assertEquals("Second level cache miss count", 0, statistics.getSecondLevelCacheMissCount());
64+
}
65+
66+
private <TPerson extends Person, TDetails extends Details> List<Serializable> createPersonsAndDetails(Class<TPerson> personClass,
67+
Class<TDetails> detailsClass) throws Exception {
68+
Session s = openSession();
69+
Transaction tx = s.beginTransaction();
70+
71+
Constructor<TPerson> ctorPerson = personClass.getConstructor();
72+
Constructor<TDetails> ctorDetails = detailsClass.getConstructor();
73+
List<Serializable> ids = new ArrayList<Serializable>();
74+
75+
for (int i = 0; i < 6; i++) {
76+
Person person = ctorPerson.newInstance();
77+
78+
if (i % 2 == 0) {
79+
Details details = ctorDetails.newInstance();
80+
81+
details.setData(String.format("%s%d", detailsClass.getName(), i));
82+
person.setDetails(details);
83+
}
84+
85+
person.setName(String.format("%s%d", personClass.getName(), i));
86+
87+
ids.add(s.save(person));
88+
}
89+
90+
tx.commit();
91+
s.close();
92+
93+
return ids;
94+
}
95+
96+
private <TPerson extends Person> List<TPerson> getPersons(Class<TPerson> personClass, List<Serializable> ids) {
97+
Session s = openSession();
98+
Transaction tx = s.beginTransaction();
99+
List<TPerson> people = new ArrayList<TPerson>();
100+
101+
for (Serializable id : ids) {
102+
people.add(s.get(personClass, id));
103+
}
104+
105+
tx.commit();
106+
s.close();
107+
108+
return people;
109+
}
110+
111+
@Test
112+
public void OneToOneCacheByForeignKey() throws Exception {
113+
OneToOneTest(PersonByFK.class, DetailsByFK.class);
114+
}
115+
116+
@Test
117+
public void OneToOneCacheByRef() throws Exception {
118+
OneToOneTest(PersonByRef.class, DetailsByRef.class);
119+
}
120+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
~ Hibernate, Relational Persistence for Idiomatic Java
4+
~
5+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
6+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
7+
-->
8+
<!DOCTYPE hibernate-mapping PUBLIC
9+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
10+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
11+
12+
<hibernate-mapping package="org.hibernate.test.onetoone.cache">
13+
<class name="PersonByFK" batch-size="3">
14+
<cache usage="read-write"/>
15+
<id name="id" unsaved-value="0">
16+
<generator class="native"/>
17+
</id>
18+
<property name="name"/>
19+
<one-to-one name="details" class="DetailsByFK" fetch="join" cascade="all"/>
20+
</class>
21+
22+
<class name="PersonByRef" batch-size="3">
23+
<cache usage="read-write"/>
24+
<id name="id" unsaved-value="0">
25+
<generator class="native"/>
26+
</id>
27+
<property name="name"/>
28+
<one-to-one name="details" class="DetailsByRef" property-ref="person" fetch="join" cascade="all"/>
29+
</class>
30+
</hibernate-mapping>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.onetoone.cache;
8+
import java.io.Serializable;
9+
10+
public abstract class Person implements Serializable {
11+
private int id;
12+
private String name;
13+
private Details details;
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public Details getDetails() {
24+
return details;
25+
}
26+
27+
public void setDetails(Details details) {
28+
if (details != null) {
29+
details.setPerson(this);
30+
}
31+
32+
this.details = details;
33+
}
34+
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.onetoone.cache;
8+
9+
public class PersonByFK extends Person {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.onetoone.cache;
8+
9+
public class PersonByRef extends Person {}

0 commit comments

Comments
 (0)