我正在使用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
时,它返回正常。现在我需要转移到本机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
注释,并且它是应该定义要执行的查询的注释,并尝试根据方法名称来解释它。
任何想法我做错了什么?
答案 0 :(得分:11)
取自Spring Data JPA(版本1.6.0.RELEASE)的文档:
@Query注释允许通过设置来执行本机查询 nativeQuery标志为true。请注意,我们目前不支持 我们正在执行对原生查询的分页或动态排序 必须操纵声明的实际查询,我们不能这样做 可靠地用于本机SQL。
很明显,本机查询不适用于分页。
因此,如果您绝对需要本机查询支持,则必须删除分页,或者您必须自行修改自定义存储库实现,您将在其中实现该功能