Skip to content

Commit 0353824

Browse files
committed
HHH-19088: Add a test that demonstrates the NPE in Envers when using StatelessSession.insert()
Signed-off-by: Mart Somermaa <[email protected]>
1 parent f230086 commit 0353824

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* SPDX-License-Identifier: LGPL-2.1-or-later
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.envers;
6+
7+
import org.hibernate.Session;
8+
import org.hibernate.SessionFactory;
9+
import org.hibernate.StatelessSession;
10+
import org.hibernate.envers.Audited;
11+
12+
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
13+
import org.hibernate.testing.orm.junit.Jpa;
14+
import org.junit.jupiter.api.Test;
15+
16+
import jakarta.persistence.Entity;
17+
import jakarta.persistence.Id;
18+
19+
@Jpa(annotatedClasses = StatelessSessionInsertTest.ToDo.class)
20+
public class StatelessSessionInsertTest {
21+
22+
/**
23+
* Reproduces the NullPointerException in Envers when insert() is called
24+
* on a stateless session.
25+
* <p>
26+
* Because of this error, Envers is not currently compatible with Jakarta Data
27+
* Repositories as the Hibernate metamodel generator generates Jakarta Data Repository
28+
* implementations with stateless sessions.
29+
* <p>
30+
* This test throws a NullPointerException with the message
31+
* "Cannot invoke \"org.hibernate.engine.spi.SessionImplementor.isTransactionInProgress()\"
32+
* because \"session\" is null"
33+
*/
34+
@Test
35+
public void test(EntityManagerFactoryScope scope) {
36+
scope.inEntityManager( entityManager -> {
37+
Session session = entityManager.unwrap( Session.class );
38+
SessionFactory sessionFactory = session.getSessionFactory();
39+
40+
try (StatelessSession statelessSession = sessionFactory.openStatelessSession()) {
41+
ToDo todo = new ToDo();
42+
todo.setId( 1L );
43+
todo.setTask( "Reproduce the Envers NullPointerException error" );
44+
45+
// insert() triggers the EnversPostInsertEventListenerImpl event listener,
46+
// which tries to call session.isTransactionInProgress() where 'session' is null.
47+
statelessSession.insert( todo );
48+
}
49+
} );
50+
}
51+
52+
@Entity
53+
@Audited
54+
public static class ToDo {
55+
@Id
56+
private Long id;
57+
58+
private String task;
59+
60+
public Long getId() {
61+
return id;
62+
}
63+
64+
public void setId(Long id) {
65+
this.id = id;
66+
}
67+
68+
public String getTask() {
69+
return task;
70+
}
71+
72+
public void setTask(String task) {
73+
this.task = task;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)