spring – 将@Query切换为nativeQuery会导致PropertyReferenceException

前端之家收集整理的这篇文章主要介绍了spring – 将@Query切换为nativeQuery会导致PropertyReferenceException前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我正在使用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 JpaRepositoryBox2D(ST_MakePoint(:swLongitude,:swLatitude),ST_MakePoint(:neLongitude,:neLatitude)),4326),location)",nativeQuery = true)
    Page

回到查询是HQL并且没有将nativeQuery设置为true时,它工作正常.现在我需要转移到本机SQL查询,虽然添加nativeQuery = true并重写查询解决它.

但是,我现在得到:

Caused by: org.springframework.data.mapping.PropertyReferenceException: No property qwerty found for type Event!
at org.springframework.data.mapping.PropertyPath.301)
at org.springframework.data.repository.query.parser.PartTree.factorybeanSupport.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.

很明显,本机查询不适用于分页.

因此,如果您绝对需要本机查询支持,则必须删除分页,或者您必须自定义自定义存储库实现,您将在其中自行实现该功能

猜你在找的Spring相关文章