From aa83b9d41dc5685943ea39b11ff0ba1bb15cc8fc Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 18 Dec 2024 13:30:34 +0200 Subject: [PATCH] fix: Allow using a custom JpaFilterConverter Fixes https://github.com/vaadin/hilla/issues/2569 --- .../spring/SpringBootAutoConfiguration.java | 8 ++++++ .../flow/spring/data/JpaFilterConverter.java | 26 ++++++++++--------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringBootAutoConfiguration.java b/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringBootAutoConfiguration.java index a9bd2bdddfa..8b0cbcbb8cc 100644 --- a/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringBootAutoConfiguration.java +++ b/vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringBootAutoConfiguration.java @@ -37,8 +37,10 @@ import com.vaadin.flow.server.Constants; import com.vaadin.flow.server.VaadinServlet; +import com.vaadin.flow.spring.data.JpaFilterConverter; import com.vaadin.flow.spring.springnative.VaadinBeanFactoryInitializationAotProcessor; +import jakarta.persistence.EntityManager; import jakarta.servlet.MultipartConfigElement; /** @@ -142,4 +144,10 @@ public ServerEndpointExporter websocketEndpointDeployer() { return new VaadinWebsocketEndpointExporter(); } + @Bean + @ConditionalOnMissingBean + public JpaFilterConverter jpaFilterConverter(EntityManager entityManager) { + return new JpaFilterConverter(entityManager); + } + } diff --git a/vaadin-spring/src/main/java/com/vaadin/flow/spring/data/JpaFilterConverter.java b/vaadin-spring/src/main/java/com/vaadin/flow/spring/data/JpaFilterConverter.java index aae2e667199..012ae095815 100644 --- a/vaadin-spring/src/main/java/com/vaadin/flow/spring/data/JpaFilterConverter.java +++ b/vaadin-spring/src/main/java/com/vaadin/flow/spring/data/JpaFilterConverter.java @@ -1,16 +1,15 @@ package com.vaadin.flow.spring.data; import jakarta.persistence.EntityManager; +import jakarta.persistence.criteria.Path; +import jakarta.persistence.criteria.Root; + +import org.springframework.data.jpa.domain.Specification; import com.vaadin.flow.spring.data.filter.AndFilter; import com.vaadin.flow.spring.data.filter.Filter; import com.vaadin.flow.spring.data.filter.OrFilter; import com.vaadin.flow.spring.data.filter.PropertyStringFilter; -import jakarta.persistence.criteria.Path; -import jakarta.persistence.criteria.Root; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.stereotype.Component; /** * Utility class for converting Hilla {@link Filter} specifications into JPA @@ -22,11 +21,14 @@ * thus should be injected into the bean that wants to use the converter. * Manually creating new instances of this class will not work. */ -@Component public class JpaFilterConverter { - @Autowired - private EntityManager em; + private EntityManager entityManager; + + /** Creates a new converter using the given entity manager. */ + public JpaFilterConverter(EntityManager entityManager) { + this.entityManager = entityManager; + } /** * Converts the given Hilla filter specification into a JPA filter @@ -72,8 +74,8 @@ private Class extractPropertyJavaType(Class entity, String propertyId) { if (propertyId.contains(".")) { String[] parts = propertyId.split("\\."); - Root root = em.getCriteriaBuilder().createQuery(entity) - .from(entity); + Root root = entityManager.getCriteriaBuilder() + .createQuery(entity).from(entity); Path path = root.get(parts[0]); int i = 1; while (i < parts.length) { @@ -82,8 +84,8 @@ private Class extractPropertyJavaType(Class entity, } return path.getJavaType(); } else { - return em.getMetamodel().entity(entity).getAttribute(propertyId) - .getJavaType(); + return entityManager.getMetamodel().entity(entity) + .getAttribute(propertyId).getJavaType(); } }