spring - 将 @Query 切换到 nativeQuery 会导致 PropertyReferenceException

标签 spring jpa spring-data

我将 Spring JPA 与 Hibernate 和 PostgreSQL 结合使用。

我有以下 JPA 存储库:

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.UUID;

public interface EventRepository extends JpaRepository<Event, UUID> {
    @Query(value = "Select * From event Where ST_Intersects(ST_SetSRID(ST_MakeBox2D(ST_MakePoint(:swLongitude, :swLatitude), ST_MakePoint(:neLongitude, :neLatitude)), 4326), location)", nativeQuery = true)
    Page<Event> qwerty(@Param("swLatitude") double swLatitude, @Param("swLongitude") double swLongitude, @Param("neLatitude") double neLatitude, @Param("neLongitude") double neLongitude, Pageable pageable);
}

当查询是 HQL 并且没有将 nativeQuery 设置为 true 时,它工作得很好。现在我需要转向 native SQL 查询,尽管添加 nativeQuery = true 并重写查询可以解决该问题。

但是,我现在得到:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property qwerty found for type Event!
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:241)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:76)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:213)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:321)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:301)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:82)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:60)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:168)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:320)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549)
... 34 more

显然,它以前不叫qwerty;我只是将其重命名以更好地说明这一点。

它似乎以某种方式忽略了 @Query 注释,它是应该定义要执行的查询的注释,并尝试根据方法名称来解释它。

知道我做错了什么吗?

最佳答案

取自Spring Data JPA(版本1.6.0.RELEASE)的文档:

The @Query annotation allows to execute native queries by setting the nativeQuery flag to true. Note, that we currently don't support execution of pagination or dynamic sorting for native queries as we'd have to manipulate the actual query declared and we cannot do this reliably for native SQL.

很明显, native 查询不适用于分页。

因此,如果您绝对需要 native 查询支持,则必须放弃分页,或者必须正确设置自定义存储库实现,在其中您将自己实现该功能

关于spring - 将 @Query 切换到 nativeQuery 会导致 PropertyReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24231632/

相关文章:

java - ejb3 : mapping many-to-many relationship jointable with a simple primary key

java - 2.0.9 和 2.1.0 之间的 Spring Boot eclipselink ddl 自动生成问题

spring - Grails:是否可以避免延迟服务初始化?

spring - Spring boot 2.0 响应式(Reactive) webflux 配置中的默认线程数

Spring Data REST @Idclass 无法识别

java - 如何让EntityManager在DAO Factory中正常工作?

java - 使用命令运行Spring Boot多模块maven项目

java - Spring Boot中使用mysql数据库访问数据

java - Spring 启动。尝试打印非存储实体时没有代理

java - spring-data-hazelcast @Query 注释给出 NullPointerException