HHH-15045 Add additional test

This commit is contained in:
Andrea Boriero 2022-05-26 14:33:06 +02:00 committed by Andrea Boriero
parent 063cb0ccd9
commit 15c4b24842
5 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?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 default-lazy="false" package="org.hibernate.orm.test.onetoone.cache">
<class name="MainObject" table="mainobject">
<cache usage="read-write"/>
<id name="id" column="id" type="java.lang.Long">
<generator class="native">
<param name="sequence">seq_mainobj</param>
</generator>
</id>
<one-to-one name="obj2" class="Object2" cascade="all" outer-join="auto"/>
<property name="description" type="java.lang.String" update="true" insert="true" column="description" />
</class>
</hibernate-mapping>

View File

@ -0,0 +1,45 @@
/*
* 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.orm.test.onetoone.cache;
/**
* @author Wolfgang Voelkl
*/
public class MainObject {
private Long id;
private String description;
private Object2 obj2;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Object2 getObj2() {
return obj2;
}
public String getDescription() {
return description;
}
public void setDescription(String string) {
description = string;
}
public void setObj2(Object2 object2) {
this.obj2 = object2;
if (object2 != null) {
object2.setBelongsToMainObj(this);
}
}
}

View File

@ -0,0 +1,28 @@
<?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 default-lazy="false" package="org.hibernate.orm.test.onetoone.cache">
<class name="Object2" table="object2">
<cache usage="read-write"/>
<id name="id" column="id" type="java.lang.Long">
<generator class="foreign">
<param name="property">belongsToMainObj</param>
</generator>
</id>
<property name="dummy" type="java.lang.String" update="true" insert="true" column="xdummy"/>
<one-to-one name="belongsToMainObj" class="MainObject" cascade="none" outer-join="auto" constrained="true"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,44 @@
/*
* 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.orm.test.onetoone.cache;
/**
*
* @author Wolfgang Voelkl
*
*/
public class Object2 {
private Long id;
private String dummy;
private MainObject belongsToMainObj;
public Long getId() {
return id;
}
public void setId(Long l) {
this.id = l;
}
public String getDummy() {
return dummy;
}
public void setDummy(String string) {
dummy = string;
}
public MainObject getBelongsToMainObj() {
return belongsToMainObj;
}
public void setBelongsToMainObj(MainObject object) {
belongsToMainObj = object;
}
}

View File

@ -0,0 +1,102 @@
/*
* 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.orm.test.onetoone.cache;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
/**
* Simple testcase to illustrate HB-992
*
* @author Wolfgang Voelkl, michael
*/
@DomainModel(
xmlMappings = { "org/hibernate/orm/test/onetoone/cache/Object2.hbm.xml", "org/hibernate/orm/test/onetoone/cache/MainObject.hbm.xml" }
)
@SessionFactory
public class OneToOneConstrainedCacheTest {
@Test
public void testOneToOneCache(SessionFactoryScope scope) {
//create a new MainObject
Object mainObjectId = createMainObject( scope );
// load the MainObject
readMainObject( mainObjectId, scope );
//create and add Ojbect2
addObject2( mainObjectId, scope );
//here the newly created Object2 is written to the database
//but the MainObject does not know it yet
MainObject mainObject = readMainObject( mainObjectId, scope );
assertNotNull( mainObject.getObj2() );
// after evicting, it works.
scope.getSessionFactory().getCache().evictEntityData( MainObject.class );
mainObject = readMainObject( mainObjectId, scope );
assertNotNull( mainObject.getObj2() );
}
/**
* creates a new MainObject
* <p/>
* one hibernate transaction !
*/
private Object createMainObject(SessionFactoryScope scope) {
MainObject mainObject = scope.fromTransaction(
session -> {
MainObject mo = new MainObject();
mo.setDescription( "Main Test" );
session.persist( mo );
return mo;
}
);
return mainObject.getId();
}
/**
* loads the newly created MainObject
* and adds a new Object2 to it
* <p/>
* one hibernate transaction
*/
private void addObject2(Object mainObjectId, SessionFactoryScope scope) {
scope.inTransaction(
session -> {
MainObject mo = session.getReference( MainObject.class, mainObjectId );
Object2 toAdd = new Object2();
toAdd.setDummy( "test" );
mo.setObj2( toAdd );
}
);
}
/**
* reads the newly created MainObject
* and its Object2 if it exists
* <p/>
* one hibernate transaction
*/
private MainObject readMainObject(Object id, SessionFactoryScope scope) {
return scope.fromTransaction(
session ->
session.get( MainObject.class, id )
);
}
}