Skip to content

Commit 15c4b24

Browse files
committed
HHH-15045 Add additional test
1 parent 063cb0c commit 15c4b24

File tree

5 files changed

+248
-0
lines changed

5 files changed

+248
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0"?>
2+
3+
<!--
4+
~ Hibernate, Relational Persistence for Idiomatic Java
5+
~
6+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
7+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
8+
-->
9+
<!DOCTYPE hibernate-mapping PUBLIC
10+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
11+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
12+
13+
<hibernate-mapping default-lazy="false" package="org.hibernate.orm.test.onetoone.cache">
14+
<class name="MainObject" table="mainobject">
15+
<cache usage="read-write"/>
16+
17+
<id name="id" column="id" type="java.lang.Long">
18+
<generator class="native">
19+
<param name="sequence">seq_mainobj</param>
20+
</generator>
21+
</id>
22+
23+
<one-to-one name="obj2" class="Object2" cascade="all" outer-join="auto"/>
24+
25+
<property name="description" type="java.lang.String" update="true" insert="true" column="description" />
26+
27+
</class>
28+
29+
</hibernate-mapping>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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.orm.test.onetoone.cache;
8+
9+
10+
/**
11+
* @author Wolfgang Voelkl
12+
*/
13+
public class MainObject {
14+
private Long id;
15+
private String description;
16+
private Object2 obj2;
17+
18+
public Long getId() {
19+
return id;
20+
}
21+
22+
public void setId(Long id) {
23+
this.id = id;
24+
}
25+
26+
public Object2 getObj2() {
27+
return obj2;
28+
}
29+
30+
public String getDescription() {
31+
return description;
32+
}
33+
34+
public void setDescription(String string) {
35+
description = string;
36+
}
37+
38+
public void setObj2(Object2 object2) {
39+
this.obj2 = object2;
40+
if (object2 != null) {
41+
object2.setBelongsToMainObj(this);
42+
}
43+
}
44+
45+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
3+
<!--
4+
~ Hibernate, Relational Persistence for Idiomatic Java
5+
~
6+
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
7+
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
8+
-->
9+
<!DOCTYPE hibernate-mapping PUBLIC
10+
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
11+
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
12+
13+
<hibernate-mapping default-lazy="false" package="org.hibernate.orm.test.onetoone.cache">
14+
<class name="Object2" table="object2">
15+
<cache usage="read-write"/>
16+
17+
<id name="id" column="id" type="java.lang.Long">
18+
<generator class="foreign">
19+
<param name="property">belongsToMainObj</param>
20+
</generator>
21+
</id>
22+
23+
<property name="dummy" type="java.lang.String" update="true" insert="true" column="xdummy"/>
24+
25+
<one-to-one name="belongsToMainObj" class="MainObject" cascade="none" outer-join="auto" constrained="true"/>
26+
</class>
27+
28+
</hibernate-mapping>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.orm.test.onetoone.cache;
8+
9+
10+
/**
11+
*
12+
* @author Wolfgang Voelkl
13+
*
14+
*/
15+
public class Object2 {
16+
private Long id;
17+
private String dummy;
18+
private MainObject belongsToMainObj;
19+
20+
public Long getId() {
21+
return id;
22+
}
23+
24+
public void setId(Long l) {
25+
this.id = l;
26+
}
27+
28+
public String getDummy() {
29+
return dummy;
30+
}
31+
32+
public void setDummy(String string) {
33+
dummy = string;
34+
}
35+
36+
public MainObject getBelongsToMainObj() {
37+
return belongsToMainObj;
38+
}
39+
40+
public void setBelongsToMainObj(MainObject object) {
41+
belongsToMainObj = object;
42+
}
43+
44+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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.orm.test.onetoone.cache;
8+
9+
import org.hibernate.testing.orm.junit.DomainModel;
10+
import org.hibernate.testing.orm.junit.SessionFactory;
11+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
12+
import org.junit.jupiter.api.Test;
13+
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
15+
16+
17+
/**
18+
* Simple testcase to illustrate HB-992
19+
*
20+
* @author Wolfgang Voelkl, michael
21+
*/
22+
@DomainModel(
23+
xmlMappings = { "org/hibernate/orm/test/onetoone/cache/Object2.hbm.xml", "org/hibernate/orm/test/onetoone/cache/MainObject.hbm.xml" }
24+
)
25+
@SessionFactory
26+
public class OneToOneConstrainedCacheTest {
27+
28+
@Test
29+
public void testOneToOneCache(SessionFactoryScope scope) {
30+
31+
//create a new MainObject
32+
Object mainObjectId = createMainObject( scope );
33+
// load the MainObject
34+
readMainObject( mainObjectId, scope );
35+
36+
//create and add Ojbect2
37+
addObject2( mainObjectId, scope );
38+
39+
//here the newly created Object2 is written to the database
40+
//but the MainObject does not know it yet
41+
MainObject mainObject = readMainObject( mainObjectId, scope );
42+
43+
assertNotNull( mainObject.getObj2() );
44+
45+
// after evicting, it works.
46+
scope.getSessionFactory().getCache().evictEntityData( MainObject.class );
47+
48+
mainObject = readMainObject( mainObjectId, scope );
49+
50+
assertNotNull( mainObject.getObj2() );
51+
}
52+
53+
/**
54+
* creates a new MainObject
55+
* <p/>
56+
* one hibernate transaction !
57+
*/
58+
private Object createMainObject(SessionFactoryScope scope) {
59+
MainObject mainObject = scope.fromTransaction(
60+
session -> {
61+
MainObject mo = new MainObject();
62+
mo.setDescription( "Main Test" );
63+
64+
session.persist( mo );
65+
return mo;
66+
}
67+
);
68+
return mainObject.getId();
69+
}
70+
71+
/**
72+
* loads the newly created MainObject
73+
* and adds a new Object2 to it
74+
* <p/>
75+
* one hibernate transaction
76+
*/
77+
private void addObject2(Object mainObjectId, SessionFactoryScope scope) {
78+
scope.inTransaction(
79+
session -> {
80+
MainObject mo = session.getReference( MainObject.class, mainObjectId );
81+
82+
Object2 toAdd = new Object2();
83+
toAdd.setDummy( "test" );
84+
85+
mo.setObj2( toAdd );
86+
}
87+
);
88+
}
89+
90+
/**
91+
* reads the newly created MainObject
92+
* and its Object2 if it exists
93+
* <p/>
94+
* one hibernate transaction
95+
*/
96+
private MainObject readMainObject(Object id, SessionFactoryScope scope) {
97+
return scope.fromTransaction(
98+
session ->
99+
session.get( MainObject.class, id )
100+
);
101+
}
102+
}

0 commit comments

Comments
 (0)