Skip to content

Commit d31a03c

Browse files
cigalyschauder
authored andcommitted
Improve robustness of HibernateJpaParametersParameterAccessor.
This change avoids errors when no transactional EntityManager is available. Closes #2540 Original pull request #2542
1 parent 0cef764 commit d31a03c

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

src/main/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import javax.persistence.EntityManager;
1919

20-
import org.hibernate.Session;
20+
import org.hibernate.SessionFactory;
2121
import org.hibernate.TypeHelper;
2222
import org.hibernate.jpa.TypedParameterValue;
2323
import org.hibernate.type.Type;
@@ -50,8 +50,7 @@ class HibernateJpaParametersParameterAccessor extends JpaParametersParameterAcce
5050

5151
super(parameters, values);
5252

53-
Session session = em.unwrap(Session.class);
54-
this.typeHelper = session.getSessionFactory().getTypeHelper();
53+
this.typeHelper = em.getEntityManagerFactory().unwrap(SessionFactory.class).getTypeHelper();
5554
}
5655

5756
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.springframework.data.jpa.provider;
2+
3+
import java.lang.reflect.Method;
4+
5+
import javax.persistence.EntityManager;
6+
7+
import org.junit.jupiter.api.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.data.jpa.repository.query.JpaParameters;
12+
import org.springframework.test.context.ContextConfiguration;
13+
import org.springframework.test.context.junit.jupiter.SpringExtension;
14+
import org.springframework.transaction.PlatformTransactionManager;
15+
import org.springframework.transaction.TransactionStatus;
16+
import org.springframework.transaction.support.DefaultTransactionDefinition;
17+
18+
@ExtendWith(SpringExtension.class)
19+
@ContextConfiguration("classpath:hjppa-test.xml")
20+
public class HibernateJpaParametersParameterAccessorUnitTests {
21+
22+
@Autowired private EntityManager em;
23+
24+
@Autowired private PlatformTransactionManager transactionManager;
25+
26+
@Test
27+
void withoutTransaction() throws NoSuchMethodException {
28+
simpleTest();
29+
}
30+
31+
@Test
32+
void withinTransaction() throws Exception {
33+
final TransactionStatus tx = transactionManager.getTransaction(new DefaultTransactionDefinition());
34+
try {
35+
simpleTest();
36+
} finally {
37+
transactionManager.rollback(tx);
38+
}
39+
}
40+
41+
private void simpleTest() throws NoSuchMethodException {
42+
final Method method = EntityManager.class.getMethod("flush");
43+
final JpaParameters parameters = new JpaParameters(method);
44+
final HibernateJpaParametersParameterAccessor accessor = new HibernateJpaParametersParameterAccessor(parameters,
45+
new Object[] {}, em);
46+
Assertions.assertEquals(0, accessor.getValues().length);
47+
Assertions.assertEquals(parameters, accessor.getParameters());
48+
}
49+
}

src/test/resources/hjppa-test.xml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
5+
xsi:schemaLocation="http://www.springframework.org/schema/jdbc https://www.springframework.org/schema/jdbc/spring-jdbc.xsd
6+
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">
7+
8+
<bean id="entityManagerFactory"
9+
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
10+
<property name="dataSource" ref="dataSource" />
11+
<property name="packagesToScan">
12+
<array>
13+
<value>org.springframework.data.jpa.domain</value>
14+
<value>org.springframework.data.jpa.domain.sample</value>
15+
</array>
16+
</property>
17+
</bean>
18+
19+
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
20+
<property name="entityManagerFactory" ref="entityManagerFactory" />
21+
</bean>
22+
23+
<bean id="defaultEntityManager"
24+
class="org.springframework.orm.jpa.support.SharedEntityManagerBean">
25+
<property name="entityManagerFactory" ref="entityManagerFactory" />
26+
<property name="synchronizedWithTransaction" value="true" />
27+
</bean>
28+
29+
<jdbc:embedded-database id="dataSource" type="HSQL" generate-name="true">
30+
<jdbc:script execution="INIT" separator="/;" location="classpath:scripts/hsqldb-init.sql"/>
31+
<jdbc:script execution="INIT" separator="/;" location="classpath:scripts/schema-stored-procedures.sql"/>
32+
</jdbc:embedded-database>
33+
</beans>

0 commit comments

Comments
 (0)