【问题标题】:org.hibernate.hql.internal.ast.QuerySyntaxEx`ception: expecting OPEN, found 'from' near line 1, columnorg.hibernate.hql.internal.ast.QuerySyntaxEx`ception:期待 OPEN,在第 1 行列附近找到“来自”
【发布时间】:2019-04-25 22:24:07
【问题描述】:

我在 Spring Boot 1.5 中有一个带有 mysql 数据库的项目。我有 2 个实体类 BackupOTPOTP,我想使用 HQL 将数据从 OTP 表复制到 BackupOTP 表。为此,我编写了这段代码。

Query query=session.createQuery("insert into BackupOTP from OTP where isExpired=:boolean");
query.setBoolean("boolean", true);
int i=query.executeUpdate();
System.err.println("i = "+i);

但我遇到以下异常:

org.hibernate.hql.internal.ast.QuerySyntaxException: 
expecting OPEN, found 'from' near line 1, column 23 
[insert into BackupOTP from com.altafjava.central.entity.OTP where isExpired=:boolean]

如何解决这个问题?

【问题讨论】:

  • 为什么要使用 where 子句进行插入?
  • 这是我们的要求所必需的。如果 OTP 已过期,那么我们需要备份,否则我们不想备份,这就是我们采用 isExpired=true 的原因
  • 这不是有效的 HQL。这是插入语句的文档:docs.jboss.org/hibernate/orm/current/userguide/html_single/…。当您阅读文档时,一切都会变得更加简洁。
  • 投反对票的原因是什么?
  • 原因是您没有先做最基本的研究就问了:阅读有关插入语句的文档。

标签: java mysql hibernate hql


【解决方案1】:

终于,我得到了答案。

实际上,这是 HQL 语法的问题。我的 HQL 语法错误。我查看了Hibernate insert query documentation 并像这样修改了我的插入语法

Query query=session.createQuery("insert into BackupOTP (otpId, createdTime, encryptedOTP, isExpired, updatedTime)"
+ " select otpId, createdTime, encryptedOTP, isExpired, updatedTime from OTP where isExpired=:boolean");

query.setBoolean("boolean", true);
int i=query.executeUpdate();
System.err.println("i = "+i);

现在它正在工作。

【讨论】:

    猜你喜欢
    • 2019-12-01
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 2012-06-22
    • 2019-08-09
    • 1970-01-01
    • 2013-02-22
    相关资源
    最近更新 更多