【问题标题】:Get Query is not working when using HQL in Hibernate Spatial在 Hibernate Spatial 中使用 HQL 时获取查询不起作用
【发布时间】:2020-02-29 11:09:19
【问题描述】:

我正在使用 Spring Boot 和 Hibernate 进行空间查询。 使用的库: 1. Spring Boot - 2.1.3.RELEASE 2. Hibernate Spatial - 5.3.7.Final 3. MariaDB - mysql Ver 15.1 Distrib 10.1.36-MariaDB

每当我在查询中使用如下所示的 HQL 时,我在应用程序启动期间收到以下错误,但是当我尝试使用本机查询时它可以工作。

我尝试过使用不同的方言。另外,尝试使用 columnDefinition 值作为几何,geolatte-geometry。

pom.xml

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-spatial</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.postgresql</groupId>
          <artifactId>postgresql</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

application.yml

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        jdbc:
          lob:
            non_contextual_creation: true
        physical_naming_strategy: com.orange.alc.polygon.dao.config.DefaultNamingStrategy 
        format_sql: false
        dialect: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect 

@Entity
public class PolygonMasterEntity {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  // Here we have used Geolatte library
  private Polygon geometry;

  @Column(name = "is_active")
  private Boolean active;

  @Column(name = "is_deleted")
  private Boolean deleted;
}

@Repository
public interface PolygonMasterRepository extends JpaRepository<PolygonMasterEntity, Long>,
    JpaSpecificationExecutor<PolygonMasterEntity> {

  @Query("select master from #{#entityName} master WHERE" 
      + " and within(master.geometry, :point)")
  List<PolygonMasterEntity> findCostUsingPointForLLME(
      @Param("point") Point point);

}

目前,我在启动过程中遇到以下错误:

原因:org.hibernate.hql.internal.ast.QuerySyntaxException:意外的 AST 节点:(靠近第 1 行,第 164 列 [从 com.orange.alc.polygon.dao.entity.PolygonMasterEntity 中选择主节点( master.geometry, :point)]

~~~~~~~~~   at org.hibernate.hql.internal.ast.ErrorTracker.throwQueryException(ErrorTracker.java:93)
~~~~~~~~~   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:277)
~~~~~~~~~   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:191)
~~~~~~~~~   at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:143)
~~~~~~~~~   at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:119)
~~~~~~~~~   at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
~~~~~~~~~   at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:153)
~~~~~~~~~   at org.hibernate.internal.AbstractSharedSessionContract.getQueryPlan(AbstractSharedSessionContract.java:595)
~~~~~~~~~   at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:704)
~~~~~~~~~   ... 100 common frames omitted


【问题讨论】:

  • 您不能使用: 来分隔spring.jpa.properties 的属性,它们必须使用.。这些属性按原样使用,因此不支持:。所以请改用hibernate.dialect 等等。这适用于spring.jpa.properties 命名空间下的所有属性。见github.com/spring-projects/spring-boot/issues/17975
  • 另外,我已经对其进行了进一步调试,异常发生在包 org.hibernate.hql.internal.antlr 下的 HqlSqlBaseWalker 类中。它没有任何空间方法的标记。在行 org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2146)
  • 因为映射了错误的方言,这是由于使用了错误的属性。

标签: spring hibernate spring-data-jpa mariadb hibernate-spatial


【解决方案1】:

spring.jpa.properties 下的属性按原样使用,并作为Map 直接传递给 JPA。由于 Spring (Boot) 不解析这些,因此您不能使用正常的 YAML 语法,而必须将属性名称与. 一样包含在内。这也解释了in this Github issue 和Spring Boot Documentation。

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: none
    properties:
      hibernate.jdbc.lob.non_contextual_creation: true
      hibernate.physical_naming_strategy: com.orange.alc.polygon.dao.config.DefaultNamingStrategy 
      hibernate.format_sql: false
      hibernate.dialect: org.hibernate.spatial.dialect.mysql.MySQLSpatialDialect 

应该工作并设置正确的方言。

【讨论】:

  • 感谢您的快速回复。我已按照建议进行了更改。仍然没有运气。同样的错误仍然存​​在。
  • 另外,我想用“:”添加那些属性也可以。我可以在日志中看到它。也许,一些更新解决了这个问题。
  • 不,它没有。您可以看到属性,它们根本不会映射到正确的位置。这也是记录在案的内容。
  • 尽管如此,即使在更改之后它也无法正常工作。
【解决方案2】:

您的查询中有and 位于WHERE 后面。 我认为这不是合法的 HQL,您应该放弃 and。

【讨论】:

  • 刚刚试过。即使在那之后,它也没有更正工作代码
猜你喜欢
  • 2014-10-19
  • 1970-01-01
  • 2015-04-25
  • 1970-01-01
  • 2017-07-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-18
  • 2011-08-23
相关资源
最近更新 更多