Как бы я использовал NEAR с MySQL и JPA - PullRequest
0 голосов
/ 12 мая 2018

Кажется, что NEAR не поддерживается:

Я получаю эту ошибку:

Неподдерживаемое ключевое слово 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]

Ответы [ 2 ]

0 голосов
/ 24 мая 2018

Весенние данные JPA основаны на JPA.Производные запросы создают запрос на основе API критериев JPA.near похоже на расширение SQL от MySql.Поэтому ваш анализ правилен: ближний не поддерживается для производных запросов.

Вместо этого используйте аннотацию @Query с native=true.

0 голосов
/ 23 мая 2018

Пожалуйста, попробуйте это:

Сущность

@Entity
public class GeoTest {

    @Id
    private String id; // SET/GET
    @GeoIndexed 
    private Point location; // SET/GET
}

Хранилище

public interface GeoTestRepository extends Repository<GeoTest, String> {
    List<GeoTest> findByLocationNear(Point p, Distance d);
}
...