13

我的代码无法注入以下存储库。根据堆栈跟踪,我的 HQL 语法似乎无效,但我不确定我做错了什么。

@Repository
 public interface JobSetRepository extends JpaRepository<JobSetEntity, Integer> {
    @Modifying
    @Transactional
    @Query("UPDATE JobSetEntity SET workstation = :workstation, workflowProcess = :workstation.workflowProcess WHERE id = :jobSet.id")
    public Boolean updateWorkstation(@Param("jobSet") JobSetEntity jobSet, @Param("workstation") Workstation workstation);

    @Modifying
    @Transactional
    @Query("UPDATE JobSetEntity SET workflowProcess = :workflowProcess, workstation = null WHERE id = :jobSet.id")
    public Boolean updateWorkflowProcess(@Param("jobSet") JobSetEntity jobSet, @Param("workflowProcess") WorkflowProcess workflowProcess);
}

堆栈跟踪:

12:57:58,692 ERROR main org.hibernate.hql.internal.ast.ErrorCounter:54 - <AST>:1:111: unexpected AST node: :
12:57:58,693 ERROR main org.hibernate.hql.internal.ast.ErrorCounter:50 - <AST>:1:111: unexpected AST node: :
<AST>:1:111: unexpected AST node: :
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4680)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExprLhs(HqlSqlBaseWalker.java:5114)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.addrExpr(HqlSqlBaseWalker.java:4575)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1260)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.newValue(HqlSqlBaseWalker.java:1211)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.assignment(HqlSqlBaseWalker.java:1033)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.setClause(HqlSqlBaseWalker.java:747)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.updateStatement(HqlSqlBaseWalker.java:367)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:255)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)

更新:我也收到此查询的此错误:

@Query("UPDATE OrderEntity SET inProduction = :order.inProduction WHERE OrderEntity.id = :order.id")
void updateInProduction(@Param("order") OrderEntity order);

我将我的版本更新到了最新版本:

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>

更新: 我也收到此错误:

@Query("SELECT oe FROM OrderEntity oe WHERE oe.id = :orderProductEntity.id")
OrderEntity findBy(@Param("orderProductEntity") OrderProductEntity orderProductEntity);
4

5 回答 5

16

可悲的是,您不能:order.inProduction(或:foo.bar一般情况下)。 参考

于 2013-09-25T14:19:58.247 回答
6

当你使用这个:

:workstation.workflowProcess

您正在定义一个参数,因此您不能使用该表示法,但您必须使用一个变量:

:workstation
:workflowProcess

等等

于 2013-09-26T12:40:05.027 回答
1

实际上错误在 NamedQuery 语法中,你不能像
workflowProcess = :workstation.workflowProcess那样做,
而是应该像
workflowProcess = :workflowProcess

于 2015-09-15T07:34:53.897 回答
0

我建议您将属性 nativeQuery 设置为true

@Query(value = "SELECT t.id FROM team as t JOIN project AS p ON p.team_id",
 nativeQuery = true)

于 2021-02-25T10:49:49.643 回答
0

您可以在查询中使用 SpEL!这是我的文章。这应该有效:

@Repository
 public interface JobSetRepository extends JpaRepository<JobSetEntity, Integer> {
    @Modifying
    @Transactional
    @Query("UPDATE JobSetEntity SET workstation = :workstation, workflowProcess = :#{#workstation.workflowProcess} WHERE id = :#{#jobSet.id}")
    public Boolean updateWorkstation(@Param("jobSet") JobSetEntity jobSet, @Param("workstation") Workstation workstation);

https://dev.to/tolgee_i18n/using-param-object-in-spring-data-jpa-query-poe

于 2022-01-14T14:16:07.220 回答