【发布时间】:2019-04-25 22:24:07
【问题描述】:
我在 Spring Boot 1.5 中有一个带有 mysql 数据库的项目。我有 2 个实体类 BackupOTP 和 OTP,我想使用 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/…。当您阅读文档时,一切都会变得更加简洁。
-
投反对票的原因是什么?
-
原因是您没有先做最基本的研究就问了:阅读有关插入语句的文档。