简体   繁体   English

用于更新对象的 JPQL 查询

[英]JPQL query for updating an object

I am trying to update a variable named endDate, which is of type LocalDate, using jpql.我正在尝试使用 jpql 更新名为 endDate 的变量,该变量属于 LocalDate 类型。 My query is as follow:我的查询如下:

@Modifying
@Query("UPDATE TeamEntity t SET t.coach.endDate = ?2 WHERE t.coach.id = ?1")
public void updateCoachEndDate(long coachId, LocalDate endDate);

part of TeamEntity class is as follow TeamEntity 类的一部分如下

@Entity
public class TeamEntity{

    @ManyToOne
    @JoinColumn(name = "coach_id", nullable = true)
    private CoachEntity coach;

} }

part of CoachEntity class is as follow CoachEntity 类的一部分如下

@Entity
public class CoachEntity{
    @Id
    private long id;
    private LocalDate endDate
}

When I run a test for it, it seems that it doesn't like当我为它运行测试时,它似乎不喜欢

r.coach.endDate = ?2

and it gives me the following error它给了我以下错误

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Syntax error in SQL statement "UPDATE Team CROSS[*] JOIN  SET END_DATE=? WHERE COACH_ID=? "; expected "., AS, SET"; SQL statement:
update team cross join  set end_date=? where coach_id=? [42001-199]

I just wondering if anyone know how to fix this error.我只是想知道是否有人知道如何解决此错误。

Your descriptions don't match, ie I'm unsure where "staff" comes from.您的描述不匹配,即我不确定“员工”来自哪里。 Anyhow:无论如何:

The Hibernate documentation says:休眠文档说:

No join, either implicit or explicit, can be specified in a bulk HQL query.不能在批量 HQL 查询中指定隐式或显式连接。 Sub-queries can be used in the where-clause, where the subqueries themselves may contain joins.子查询可以用在 where 子句中,子查询本身可能包含连接。

And you have an implicit join in there between TeamEntity and Coach.您在 TeamEntity 和 Coach 之间有一个隐式连接。

So, the next question is, why go through the TeamEntity anyway, since you have the coachId?那么,下一个问题是,既然你有coachId,为什么还要通过TeamEntity?

update Coach c set c.endDate = ?2 where c.id = ?1

Should do the trick应该做的伎俩

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM