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 .bytecode .enhancement .lazy .proxy ;
8
+
9
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
10
+ import static org .hibernate .testing .transaction .TransactionUtil .doInHibernate ;
11
+
12
+ import java .util .ArrayList ;
13
+ import java .util .List ;
14
+ import javax .persistence .Entity ;
15
+ import javax .persistence .FetchType ;
16
+ import javax .persistence .Id ;
17
+ import javax .persistence .ManyToMany ;
18
+ import javax .persistence .Table ;
19
+
20
+ import org .hibernate .LazyInitializationException ;
21
+
22
+ import org .hibernate .testing .TestForIssue ;
23
+ import org .hibernate .testing .bytecode .enhancement .BytecodeEnhancerRunner ;
24
+ import org .hibernate .testing .bytecode .enhancement .EnhancementOptions ;
25
+ import org .hibernate .testing .junit4 .BaseNonConfigCoreFunctionalTestCase ;
26
+ import org .junit .After ;
27
+ import org .junit .Test ;
28
+ import org .junit .runner .RunWith ;
29
+
30
+ @ TestForIssue (jiraKey = "HHH-14811" )
31
+ @ RunWith (BytecodeEnhancerRunner .class )
32
+ @ EnhancementOptions (lazyLoading = true )
33
+ public class BytecodeEnhancedLazyLoadingOnDeletedEntityTest
34
+ extends BaseNonConfigCoreFunctionalTestCase {
35
+
36
+ @ Override
37
+ public Class <?>[] getAnnotatedClasses () {
38
+ return new Class <?>[] { AssociationOwner .class , AssociationNonOwner .class };
39
+ }
40
+
41
+ @ After
42
+ public void tearDown () {
43
+ doInHibernate ( this ::sessionFactory , s -> {
44
+ s .createQuery ( "delete from AOwner" ).executeUpdate ();
45
+ s .createQuery ( "delete from ANonOwner" ).executeUpdate ();
46
+ } );
47
+ }
48
+
49
+ @ Test
50
+ public void accessUnloadedLazyAssociationOnDeletedOwner () {
51
+ inTransaction ( s -> {
52
+ AssociationOwner owner = new AssociationOwner ();
53
+ owner .setId ( 1 );
54
+ for ( int i = 0 ; i < 2 ; i ++ ) {
55
+ AssociationNonOwner nonOwner = new AssociationNonOwner ();
56
+ nonOwner .setId ( i );
57
+ s .persist ( nonOwner );
58
+ nonOwner .getOwners ().add ( owner );
59
+ owner .getNonOwners ().add ( nonOwner );
60
+ }
61
+ s .persist ( owner );
62
+ } );
63
+ assertThatThrownBy ( () -> inTransaction ( session -> {
64
+ AssociationOwner owner = session .load ( AssociationOwner .class , 1 );
65
+ session .delete ( owner );
66
+ session .flush ();
67
+ owner .getNonOwners ().size ();
68
+ } ) )
69
+ .isInstanceOf ( LazyInitializationException .class )
70
+ .hasMessageContaining (
71
+ "Could not locate EntityEntry for the collection owner in the PersistenceContext" );
72
+ }
73
+
74
+ @ Test
75
+ public void accessUnloadedLazyAssociationOnDeletedNonOwner () {
76
+ inTransaction ( s -> {
77
+ AssociationNonOwner nonOwner = new AssociationNonOwner ();
78
+ nonOwner .setId ( 1 );
79
+ s .persist ( nonOwner );
80
+ } );
81
+ assertThatThrownBy ( () -> inTransaction ( session -> {
82
+ AssociationNonOwner nonOwner = session .load ( AssociationNonOwner .class , 1 );
83
+ session .delete ( nonOwner );
84
+ session .flush ();
85
+ nonOwner .getOwners ().size ();
86
+ } ) )
87
+ .isInstanceOf ( LazyInitializationException .class )
88
+ .hasMessageContaining (
89
+ "Could not locate EntityEntry for the collection owner in the PersistenceContext" );
90
+ }
91
+
92
+ @ Entity (name = "AOwner" )
93
+ @ Table
94
+ private static class AssociationOwner {
95
+
96
+ @ Id
97
+ Integer id ;
98
+
99
+ @ ManyToMany (fetch = FetchType .LAZY )
100
+ List <AssociationNonOwner > nonOwners = new ArrayList <>();
101
+
102
+ public Integer getId () {
103
+ return id ;
104
+ }
105
+
106
+ public void setId (Integer id ) {
107
+ this .id = id ;
108
+ }
109
+
110
+ public List <AssociationNonOwner > getNonOwners () {
111
+ return nonOwners ;
112
+ }
113
+
114
+ public void setNonOwners (
115
+ List <AssociationNonOwner > nonOwners ) {
116
+ this .nonOwners = nonOwners ;
117
+ }
118
+ }
119
+
120
+ @ Entity (name = "ANonOwner" )
121
+ @ Table
122
+ private static class AssociationNonOwner {
123
+
124
+ @ Id
125
+ Integer id ;
126
+
127
+ @ ManyToMany (mappedBy = "nonOwners" , fetch = FetchType .LAZY )
128
+ List <AssociationOwner > owners = new ArrayList <>();
129
+
130
+ AssociationNonOwner () {
131
+ }
132
+
133
+ public Integer getId () {
134
+ return id ;
135
+ }
136
+
137
+ public void setId (Integer id ) {
138
+ this .id = id ;
139
+ }
140
+
141
+ public List <AssociationOwner > getOwners () {
142
+ return owners ;
143
+ }
144
+
145
+ public void setOwners (List <AssociationOwner > owners ) {
146
+ this .owners = owners ;
147
+ }
148
+ }
149
+ }
0 commit comments