From 718f9201fca54180b87a264fee8d9ff1a9c7d302 Mon Sep 17 00:00:00 2001 From: Cedomir Igaly Date: Mon, 23 May 2022 11:19:42 +0200 Subject: [PATCH] Improve robustness of HibernateJpaParametersParameterAccessor. This change avoids errors when no transactional EntityManager is available. Closes #2540 Original pull request #2542 --- ...bernateJpaParametersParameterAccessor.java | 5 +- ...aParametersParameterAccessorUnitTests.java | 49 +++++++++++++++++++ .../src/test/resources/hjppa-test.xml | 33 +++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 spring-data-jpa/src/test/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessorUnitTests.java create mode 100644 spring-data-jpa/src/test/resources/hjppa-test.xml diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessor.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessor.java index 5948c519d..d2a5ccd1a 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessor.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessor.java @@ -16,7 +16,7 @@ package org.springframework.data.jpa.provider; import jakarta.persistence.EntityManager; -import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.hibernate.TypeHelper; import org.hibernate.jpa.TypedParameterValue; import org.hibernate.type.Type; @@ -49,8 +49,7 @@ class HibernateJpaParametersParameterAccessor extends JpaParametersParameterAcce super(parameters, values); - Session session = em.unwrap(Session.class); - this.typeHelper = session.getSessionFactory().getTypeHelper(); + this.typeHelper = em.getEntityManagerFactory().unwrap(SessionFactory.class).getTypeHelper(); } @Override diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessorUnitTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessorUnitTests.java new file mode 100644 index 000000000..a7665990c --- /dev/null +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/provider/HibernateJpaParametersParameterAccessorUnitTests.java @@ -0,0 +1,49 @@ +package org.springframework.data.jpa.provider; + +import jakarta.persistence.EntityManager; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.repository.query.JpaParameters; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; + +import java.lang.reflect.Method; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration("classpath:hjppa-test.xml") +public class HibernateJpaParametersParameterAccessorUnitTests { + + @Autowired + private EntityManager em; + + @Autowired + private PlatformTransactionManager transactionManager; + + @Test + void withoutTransaction() throws NoSuchMethodException { + simpleTest(); + } + + @Test + void withinTransaction() throws Exception { + final TransactionStatus tx = transactionManager.getTransaction(new DefaultTransactionDefinition()); + try { + simpleTest(); + } finally { + transactionManager.rollback(tx); + } + } + + private void simpleTest() throws NoSuchMethodException { + final Method method = EntityManager.class.getMethod("flush"); + final JpaParameters parameters = new JpaParameters(method); + final HibernateJpaParametersParameterAccessor accessor = new HibernateJpaParametersParameterAccessor(parameters, new Object[]{}, em); + Assertions.assertEquals(0, accessor.getValues().length); + Assertions.assertEquals(parameters, accessor.getParameters()); + } +} diff --git a/spring-data-jpa/src/test/resources/hjppa-test.xml b/spring-data-jpa/src/test/resources/hjppa-test.xml new file mode 100644 index 000000000..cec01327c --- /dev/null +++ b/spring-data-jpa/src/test/resources/hjppa-test.xml @@ -0,0 +1,33 @@ + + + + + + + + org.springframework.data.jpa.domain + org.springframework.data.jpa.domain.sample + + + + + + + + + + + + + + + + + +