Skip to content

Commit

Permalink
fix: Allow using a custom JpaFilterConverter
Browse files Browse the repository at this point in the history
  • Loading branch information
Artur- committed Dec 18, 2024
1 parent 83ece96 commit aa83b9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -142,4 +144,10 @@ public ServerEndpointExporter websocketEndpointDeployer() {
return new VaadinWebsocketEndpointExporter();
}

@Bean
@ConditionalOnMissingBean
public JpaFilterConverter jpaFilterConverter(EntityManager entityManager) {
return new JpaFilterConverter(entityManager);
}

}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}

Expand Down

0 comments on commit aa83b9d

Please sign in to comment.