Skip to content

Commit c116976

Browse files
committed
HHH-19038 - add test for issue
Signed-off-by: Jan Schatteman <[email protected]>
1 parent 90f60fa commit c116976

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.collection.basic;
6+
7+
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.Id;
10+
import jakarta.persistence.ManyToOne;
11+
import jakarta.persistence.OneToMany;
12+
import org.hibernate.Hibernate;
13+
import org.hibernate.LazyInitializationException;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.SessionFactory;
16+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.ArrayList;
20+
import java.util.HashMap;
21+
import java.util.List;
22+
import java.util.Map;
23+
24+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
26+
import static org.junit.jupiter.api.Assertions.assertFalse;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
28+
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
30+
31+
/**
32+
* @author Jan Schatteman
33+
*/
34+
@DomainModel(
35+
annotatedClasses = {DetachedElementTest.EntityWithList.class, DetachedElementTest.ListElement.class,
36+
DetachedElementTest.EntityWithMap.class, DetachedElementTest.MapElement.class}
37+
)
38+
@SessionFactory
39+
public class DetachedElementTest {
40+
41+
@Test
42+
public void testList(SessionFactoryScope scope) {
43+
scope.inSession(
44+
session -> {
45+
session.getTransaction().begin();
46+
EntityWithList parent = new EntityWithList(1);
47+
ListElement element = new ListElement(2, parent);
48+
session.persist( parent );
49+
session.persist( element );
50+
session.getTransaction().commit();
51+
session.clear();
52+
53+
// shouldn't throw exceptions for these operations
54+
assertDoesNotThrow(
55+
() -> {
56+
assertEquals( 1, parent.children.size() );
57+
assertFalse( parent.children.isEmpty() );
58+
assertTrue( parent.children.contains(element) );
59+
assertTrue( parent.children.remove(element) );
60+
}
61+
);
62+
63+
assertThrows( LazyInitializationException.class,
64+
() -> Hibernate.get(parent.children, 0)
65+
);
66+
}
67+
);
68+
}
69+
70+
@Test
71+
void testMap(SessionFactoryScope scope) {
72+
scope.inSession(
73+
session -> {
74+
session.getTransaction().begin();
75+
EntityWithMap parent = new EntityWithMap(1);
76+
MapElement element = new MapElement(2, parent);
77+
session.persist(parent);
78+
session.persist(element);
79+
session.getTransaction().commit();
80+
session.clear();
81+
82+
// shouldn't throw exceptions for these operations
83+
assertDoesNotThrow(
84+
() -> {
85+
assertEquals( 1, parent.children.size() );
86+
assertFalse( parent.children.isEmpty() );
87+
assertTrue( parent.children.containsKey(element.id) );
88+
assertTrue( parent.children.containsValue(element) );
89+
}
90+
);
91+
92+
assertThrows( LazyInitializationException.class,
93+
() -> Hibernate.get(parent.children, 2L)
94+
);
95+
}
96+
);
97+
}
98+
99+
@Entity
100+
public class EntityWithList {
101+
@Id
102+
long id;
103+
104+
@OneToMany(mappedBy = "parent")
105+
List<ListElement> children = new ArrayList<>();
106+
107+
public EntityWithList(int id) {
108+
this.id = id;
109+
}
110+
111+
protected EntityWithList() {}
112+
}
113+
114+
@Entity
115+
public class ListElement {
116+
@Id
117+
long id;
118+
119+
@ManyToOne
120+
private EntityWithList parent;
121+
122+
public ListElement(long id, EntityWithList parent) {
123+
this.id = id;
124+
this.parent = parent;
125+
parent.children.add(this);
126+
}
127+
128+
protected ListElement() {}
129+
}
130+
131+
@Entity
132+
public class EntityWithMap {
133+
@Id
134+
long id;
135+
136+
@OneToMany(mappedBy = "parent")
137+
Map<Long, MapElement> children = new HashMap<>();
138+
139+
public EntityWithMap(int id) {
140+
this.id = id;
141+
}
142+
143+
protected EntityWithMap() {}
144+
}
145+
146+
@Entity
147+
public class MapElement {
148+
@Id
149+
long id;
150+
151+
@ManyToOne
152+
private EntityWithMap parent;
153+
154+
public MapElement(long id, EntityWithMap parent) {
155+
this.id = id;
156+
this.parent = parent;
157+
parent.children.put(id, this);
158+
}
159+
160+
protected MapElement() {}
161+
}
162+
163+
}

0 commit comments

Comments
 (0)