作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
貌似不支持NEAR:
我收到这个错误:
Unsupported keyword NEAR (1): [IsNear, Near]
这是我的仓库:
public interface GeoTestRepository extends JpaRepository<GeoTest, String> {
GeoResults<GeoTest> findByLocationNear(Point p, Distance d);
}
至于MySql:
SELECT VERSION();
8.0.11
连接器的依赖是:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
我的实体是:
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Arrays;
@Entity
public class GeoTest {
@Id
private String id; // SET/GET
private GeoJsonPoint location; // SET/GET
}
下面是我得到的确切异常。
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'geoTestController': Unsatisfied dependency expressed through field 'geoTestRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geoTestRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.geo.GeoResults com.example.demo.repository.GeoTestRepository.findByLocationNear(org.springframework.data.geo.Point,org.springframework.data.geo.Distance)! Unsupported keyword NEAR (1): [IsNear, Near]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'geoTestRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.geo.GeoResults com.example.demo.repository.GeoTestRepository.findByLocationNear(org.springframework.data.geo.Point,org.springframework.data.geo.Distance)! Unsupported keyword NEAR (1): [IsNear, Near]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1706) ~[spring-beans-5.0.6.RELEASE.jar:5.0.6.RELEASE]
... 42 common frames omitted
Caused by: java.lang.IllegalArgumentException: Failed to create query for method public abstract org.springframework.data.geo.GeoResults com.example.demo.repository.GeoTestRepository.findByLocationNear(org.springframework.data.geo.Point,org.springframework.data.geo.Distance)! Unsupported keyword NEAR (1): [IsNear, Near]
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:82) ~[spring-data-jpa-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:103) ~[spring-data-jpa-2.0.7.RELEASE.jar:2.0.7.RELEASE]
... 52 common frames omitted
Caused by: java.lang.IllegalArgumentException: Unsupported keyword NEAR (1): [IsNear, Near]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:318) ~[spring-data-jpa-2.0.7.RELEASE.jar:2.0.7.RELEASE]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:206) ~[spring-data-jpa-2.0.7.RELEASE.jar:2.0.7.RELEASE]
... 78 common frames omitted
2018-05-21 15:47:36.276 ERROR 9512 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@4218d6a3] to prepare test instance [com.example.demo.test.GeoTestTest@52ca2652]
最佳答案
Spring Data JPA 基于JPA。派生查询基于 JPA Criteria API 创建查询。 near
似乎是 MySql 对 SQL 的扩展。因此,您的分析是正确的:派生查询不支持 near。
改为使用 @Query
注释和 native=true
。
关于mysql - 我如何将 NEAR 与 MySQL 和 JPA 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50307323/
我是一名优秀的程序员,十分优秀!