HHH-14216 Add one-to-one second level cache tests.

This commit is contained in:
David Ellingsworth 2020-10-15 17:54:27 -04:00 committed by Steve Ebersole
parent f45a88cb07
commit aec21d21f8
9 changed files with 300 additions and 0 deletions

View File

@ -0,0 +1,33 @@
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.onetoone.cache">
<class name="DetailsByFK">
<cache usage="read-write"/>
<id name="id" unsaved-value="0">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<one-to-one name="person" class="PersonByFK" constrained="true"/>
<property name="data"/>
</class>
<class name="DetailsByRef">
<cache usage="read-write"/>
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="person" class="PersonByRef" column="personId" unique="true"/>
<property name="data"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,38 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.cache;
import java.io.Serializable;
public abstract class Details implements Serializable {
private int id;
private String data;
private Person person;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public Person getPerson() {
return person;
}
protected void setPerson(Person person) {
this.person = person;
}
}

View File

@ -0,0 +1,9 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.cache;
public class DetailsByFK extends Details {}

View File

@ -0,0 +1,9 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.cache;
public class DetailsByRef extends Details {}

View File

@ -0,0 +1,120 @@
package org.hibernate.test.onetoone.cache;
import static org.junit.Assert.assertEquals;
import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cache.spi.CacheImplementor;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.cfg.Configuration;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.stat.spi.StatisticsImplementor;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.junit.Test;
public class OneToOneCacheTest extends BaseCoreFunctionalTestCase {
@Override
public String[] getMappings() {
return new String[] {
"onetoone/cache/Details.hbm.xml",
"onetoone/cache/Person.hbm.xml",
};
}
@Override
protected void configure(Configuration configuration) {
configuration.setProperty(AvailableSettings.USE_SECOND_LEVEL_CACHE, "true");
configuration.setProperty(AvailableSettings.GENERATE_STATISTICS, "true");
}
private <TPerson extends Person, TDetails extends Details> void OneToOneTest(Class<TPerson> personClass,
Class<TDetails> detailsClass) throws Exception {
// Initialize the database with data.
List<Serializable> ids = createPersonsAndDetails(personClass, detailsClass);
// Clear the second level cache and the statistics.
SessionFactoryImplementor sfi = sessionFactory();
CacheImplementor cache = sfi.getCache();
StatisticsImplementor statistics = sfi.getStatistics();
cache.evictEntityData(personClass);
cache.evictEntityData(detailsClass);
cache.evictQueryRegions();
statistics.clear();
// Fill the empty caches with data.
this.getPersons(personClass, ids);
// Verify that no data was retrieved from the cache.
assertEquals("Second level cache hit count", 0, statistics.getSecondLevelCacheHitCount());
statistics.clear();
this.getPersons(personClass, ids);
// Verify that all data was retrieved from the cache.
assertEquals("Second level cache miss count", 0, statistics.getSecondLevelCacheMissCount());
}
private <TPerson extends Person, TDetails extends Details> List<Serializable> createPersonsAndDetails(Class<TPerson> personClass,
Class<TDetails> detailsClass) throws Exception {
Session s = openSession();
Transaction tx = s.beginTransaction();
Constructor<TPerson> ctorPerson = personClass.getConstructor();
Constructor<TDetails> ctorDetails = detailsClass.getConstructor();
List<Serializable> ids = new ArrayList<Serializable>();
for (int i = 0; i < 6; i++) {
Person person = ctorPerson.newInstance();
if (i % 2 == 0) {
Details details = ctorDetails.newInstance();
details.setData(String.format("%s%d", detailsClass.getName(), i));
person.setDetails(details);
}
person.setName(String.format("%s%d", personClass.getName(), i));
ids.add(s.save(person));
}
tx.commit();
s.close();
return ids;
}
private <TPerson extends Person> List<TPerson> getPersons(Class<TPerson> personClass, List<Serializable> ids) {
Session s = openSession();
Transaction tx = s.beginTransaction();
List<TPerson> people = new ArrayList<TPerson>();
for (Serializable id : ids) {
people.add(s.get(personClass, id));
}
tx.commit();
s.close();
return people;
}
@Test
public void OneToOneCacheByForeignKey() throws Exception {
OneToOneTest(PersonByFK.class, DetailsByFK.class);
}
@Test
public void OneToOneCacheByRef() throws Exception {
OneToOneTest(PersonByRef.class, DetailsByRef.class);
}
}

View File

@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.onetoone.cache">
<class name="PersonByFK" batch-size="3">
<cache usage="read-write"/>
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name"/>
<one-to-one name="details" class="DetailsByFK" fetch="join" cascade="all"/>
</class>
<class name="PersonByRef" batch-size="3">
<cache usage="read-write"/>
<id name="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name"/>
<one-to-one name="details" class="DetailsByRef" property-ref="person" fetch="join" cascade="all"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,43 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.cache;
import java.io.Serializable;
public abstract class Person implements Serializable {
private int id;
private String name;
private Details details;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Details getDetails() {
return details;
}
public void setDetails(Details details) {
if (details != null) {
details.setPerson(this);
}
this.details = details;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,9 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.cache;
public class PersonByFK extends Person {}

View File

@ -0,0 +1,9 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.onetoone.cache;
public class PersonByRef extends Person {}