Skip to content

Commit f14a23e

Browse files
committed
HHH-19523 test for collection listeners in stateless session
1 parent eff4ab0 commit f14a23e

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.stateless.events;
6+
7+
import org.hibernate.engine.spi.SessionFactoryImplementor;
8+
import org.hibernate.event.service.spi.EventListenerRegistry;
9+
import org.hibernate.event.spi.EventType;
10+
import org.hibernate.event.spi.PostCollectionRecreateEvent;
11+
import org.hibernate.event.spi.PostCollectionRecreateEventListener;
12+
import org.hibernate.event.spi.PostCollectionRemoveEvent;
13+
import org.hibernate.event.spi.PostCollectionRemoveEventListener;
14+
import org.hibernate.event.spi.PreCollectionRecreateEvent;
15+
import org.hibernate.event.spi.PreCollectionRecreateEventListener;
16+
import org.hibernate.event.spi.PreCollectionRemoveEvent;
17+
import org.hibernate.event.spi.PreCollectionRemoveEventListener;
18+
import org.hibernate.testing.orm.junit.DomainModel;
19+
import org.hibernate.testing.orm.junit.SessionFactory;
20+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.junit.jupiter.api.Assertions.assertEquals;
24+
25+
@DomainModel(annotatedClasses = {EntityA.class, EntityB.class})
26+
@SessionFactory
27+
class CollectionListenerInStatelessSessionTest {
28+
29+
@Test
30+
void statelessInsert(SessionFactoryScope scope) {
31+
EventListenerRegistry registry =
32+
scope.getSessionFactory().unwrap(SessionFactoryImplementor.class)
33+
.getServiceRegistry().getService(EventListenerRegistry.class);
34+
var preRecreate = new MyPreCollectionRecreateEventListener();
35+
var preRemove = new MyPreCollectionRemoveEventListener();
36+
var postRecreate = new MyPostCollectionRecreateEventListener();
37+
var postRemove = new MyPostCollectionRemoveEventListener();
38+
registry.getEventListenerGroup(EventType.PRE_COLLECTION_RECREATE)
39+
.appendListener( preRecreate );
40+
registry.getEventListenerGroup(EventType.PRE_COLLECTION_REMOVE)
41+
.appendListener( preRemove );
42+
registry.getEventListenerGroup(EventType.POST_COLLECTION_RECREATE)
43+
.appendListener( postRecreate );
44+
registry.getEventListenerGroup(EventType.POST_COLLECTION_REMOVE)
45+
.appendListener( postRemove );
46+
47+
scope.inStatelessTransaction(statelessSession -> {
48+
EntityA a = new EntityA();
49+
EntityB b = new EntityB();
50+
a.children.add(b);
51+
statelessSession.insert( b );
52+
statelessSession.insert( a );
53+
statelessSession.delete( a );
54+
statelessSession.delete( b );
55+
});
56+
57+
assertEquals(1, preRecreate.called);
58+
assertEquals(1, preRemove.called);
59+
assertEquals(1, postRecreate.called);
60+
assertEquals(1, postRemove.called);
61+
}
62+
63+
}
64+
65+
class MyPreCollectionRecreateEventListener implements PreCollectionRecreateEventListener {
66+
67+
int called = 0;
68+
69+
@Override
70+
public void onPreRecreateCollection(PreCollectionRecreateEvent event) {
71+
called++;
72+
}
73+
74+
}
75+
76+
class MyPreCollectionRemoveEventListener implements PreCollectionRemoveEventListener {
77+
78+
int called = 0;
79+
80+
@Override
81+
public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
82+
called++;
83+
}
84+
85+
}
86+
87+
class MyPostCollectionRecreateEventListener implements PostCollectionRecreateEventListener {
88+
89+
int called = 0;
90+
91+
@Override
92+
public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
93+
called++;
94+
}
95+
96+
}
97+
98+
class MyPostCollectionRemoveEventListener implements PostCollectionRemoveEventListener {
99+
100+
int called = 0;
101+
102+
@Override
103+
public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
104+
called++;
105+
}
106+
107+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.stateless.events;
6+
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
10+
import jakarta.persistence.Column;
11+
import jakarta.persistence.Entity;
12+
import jakarta.persistence.GeneratedValue;
13+
import jakarta.persistence.GenerationType;
14+
import jakarta.persistence.Id;
15+
import jakarta.persistence.JoinColumn;
16+
import jakarta.persistence.OneToMany;
17+
import jakarta.persistence.Table;
18+
19+
@Entity
20+
@Table(name = "ENTITY_A")
21+
public class EntityA {
22+
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.AUTO)
25+
@Column(name = "ID")
26+
Integer id;
27+
28+
@OneToMany
29+
@JoinColumn(name = "ENTITY_A")
30+
Collection<EntityB> children = new ArrayList<>();
31+
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.stateless.events;
6+
7+
import jakarta.persistence.Column;
8+
import jakarta.persistence.Entity;
9+
import jakarta.persistence.GeneratedValue;
10+
import jakarta.persistence.GenerationType;
11+
import jakarta.persistence.Id;
12+
import jakarta.persistence.Table;
13+
14+
@Entity
15+
@Table(name = "ENTITY_B")
16+
public class EntityB {
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.AUTO)
19+
@Column(name = "ID")
20+
Integer id;
21+
}

0 commit comments

Comments
 (0)