Skip to content

Commit eae088a

Browse files
gavinkingyrodiere
authored andcommitted
HHH-19523 test for collection listeners in stateless session
1 parent 720d4bd commit eae088a

File tree

3 files changed

+161
-0
lines changed

3 files changed

+161
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
// Note : in 7.0, the StatelessSession does not trigger collection events
58+
assertEquals(0, preRecreate.called);
59+
assertEquals(0, preRemove.called);
60+
assertEquals(0, postRecreate.called);
61+
assertEquals(0, postRemove.called);
62+
}
63+
64+
}
65+
66+
class MyPreCollectionRecreateEventListener implements PreCollectionRecreateEventListener {
67+
68+
int called = 0;
69+
70+
@Override
71+
public void onPreRecreateCollection(PreCollectionRecreateEvent event) {
72+
called++;
73+
}
74+
75+
}
76+
77+
class MyPreCollectionRemoveEventListener implements PreCollectionRemoveEventListener {
78+
79+
int called = 0;
80+
81+
@Override
82+
public void onPreRemoveCollection(PreCollectionRemoveEvent event) {
83+
called++;
84+
}
85+
86+
}
87+
88+
class MyPostCollectionRecreateEventListener implements PostCollectionRecreateEventListener {
89+
90+
int called = 0;
91+
92+
@Override
93+
public void onPostRecreateCollection(PostCollectionRecreateEvent event) {
94+
called++;
95+
}
96+
97+
}
98+
99+
class MyPostCollectionRemoveEventListener implements PostCollectionRemoveEventListener {
100+
101+
int called = 0;
102+
103+
@Override
104+
public void onPostRemoveCollection(PostCollectionRemoveEvent event) {
105+
called++;
106+
}
107+
108+
}
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)