/
ContainedInTriggerUnnecessaryCollectionInitializationIT.java
200 lines (160 loc) · 5.07 KB
/
ContainedInTriggerUnnecessaryCollectionInitializationIT.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Hibernate Search, full-text search for your domain model
*
* 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.search.integrationtest.mapper.orm.automaticindexing.proxy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;
import static org.junit.Assert.assertFalse;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.GenericField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.IndexedEmbedded;
import org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock;
import org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmSetupHelper;
import org.hibernate.search.util.impl.test.annotation.TestForIssue;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@TestForIssue(jiraKey = "HSEARCH-1710")
public class ContainedInTriggerUnnecessaryCollectionInitializationIT {
@Rule
public BackendMock backendMock = new BackendMock();
@Rule
public OrmSetupHelper ormSetupHelper = OrmSetupHelper.withBackendMock( backendMock );
private SessionFactory sessionFactory;
@Before
public void setup() {
backendMock.expectAnySchema( Group.INDEX );
backendMock.expectAnySchema( Post.INDEX );
sessionFactory = ormSetupHelper.start()
.setup(
Group.class,
Post.class
);
backendMock.verifyExpectationsMet();
}
@Test
public void test() {
with( sessionFactory ).runInTransaction( session -> {
Group group = new Group();
group.setId( 1 );
group.setSomeField( "initialValue" );
group.setSomeInteger( 42 );
Post post = new Post();
post.setId( 2 );
post.setGroup( group );
group.getPosts().add( post );
session.persist( group );
session.persist( post );
backendMock.expectWorks( Group.INDEX )
.add( "1", b -> b
.field( "someField", "initialValue" )
.field( "someInteger", 42 )
);
backendMock.expectWorks( Post.INDEX )
.add( "2", b -> b
.objectField( "group", b2 -> b2
.field( "someInteger", 42 )
)
);
} );
backendMock.verifyExpectationsMet();
AtomicReference<Group> groupFromModifyingTransaction = new AtomicReference<>();
with( sessionFactory ).runInTransaction( session -> {
Group group = session.getReference( Group.class, 1 );
groupFromModifyingTransaction.set( group );
// The posts should not be initialized
assertThat( group.getPosts() )
.isInstanceOf( PersistentCollection.class )
.satisfies( p -> assertFalse(
"The posts should not be initialized",
Hibernate.isInitialized( p )
) );
group.setSomeField( "updatedValue" );
backendMock.expectWorks( Group.INDEX )
.addOrUpdate( "1", b -> b
.field( "someField", "updatedValue" )
.field( "someInteger", 42 )
);
// Do not expect any work on the Post index
} );
backendMock.verifyExpectationsMet();
Group group = groupFromModifyingTransaction.get();
assertThat( group.getPosts() )
.isInstanceOf( PersistentCollection.class )
.satisfies( p -> assertFalse(
"The posts should not be initialized by Hibernate Search",
Hibernate.isInitialized( p )
) );
}
@Entity(name = "Group_")
@Indexed(index = Group.INDEX)
public static class Group {
public static final String INDEX = "Group";
@Id
private Integer id;
@GenericField
private String someField;
@GenericField
private int someInteger;
@OneToMany(mappedBy = "group")
@SuppressWarnings("deprecation")
@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.EXTRA)
private Set<Post> posts = new HashSet<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSomeField() {
return someField;
}
public void setSomeField(String someField) {
this.someField = someField;
}
public int isSomeInteger() {
return someInteger;
}
public void setSomeInteger(int someInteger) {
this.someInteger = someInteger;
}
public Set<Post> getPosts() {
return posts;
}
}
@Entity(name = "Post")
@Indexed(index = Post.INDEX)
public static class Post {
public static final String INDEX = "Post";
@Id
private Integer id;
@ManyToOne
@IndexedEmbedded(includePaths = { "someInteger" })
private Group group;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
}
}