org.hibernate.hql.internal.QueryExecutionRequestException:DML操作不支持[插入到

时间:2016-04-07 12:20:03

标签: java hibernate

我正在从一个表中获取数据并希望插入到其他表中。 第一个表包含许多字段,如果要存储在新表中,我只需要一些字段。为此,我正在使用Hibernate查询。 我能够从查询中获取结果,但是当我尝试插入其他表时,它会抛出

  

org.hibernate.hql.internal.QueryExecutionRequestException:DML操作不支持[insert into

以下是抛出异常的代码。

Query query=session.createQuery(" insert into CustomerMailAddress (subs_id,mail_Id) SELECT DISTINCT  subs_id, email_id FROM SubcriberModel WHERE subs_id IS NOT NULL AND email_id IS NOT NULL AND email_id <> ''");
List result = query.list();
int res = query.executeUpdate();
System.out.println("Command successfully executed....");
System.out.println("Numer of records effected...,"+result.size());

当我在不使用query.executeUpdate()的情况下运行代码时;方法

Query query=session.createQuery(" SELECT DISTINCT  subs_id, email_id FROM SubcriberModel WHERE subs_id IS NOT NULL AND email_id IS NOT NULL AND email_id <> ''  UNION ALL SELECT DISTINCT tbl_subscribers_subs_id, email_id FROM SocialProfileModel WHERE tbl_subscribers_subs_id IS NOT NULL AND email_id IS NOT NULL AND email_id <> ''");
List result = query.list();

以上代码的输出是 命令成功执行.... 记录的数量影响...,21。

1 个答案:

答案 0 :(得分:2)

如果您正在进行插入或更新,则只应致电

query.executeUpdate();

而不是

query.list();

尝试这样(你在选择部分中缺少别名,因此Hibernate对subs_id感到困惑):

Query query=session.createQuery("insert into CustomerMailAddress (subs_id, mail_Id)"+"SELECT DISTINCT m.subs_id, m.email_id FROM SubcriberModel m WHERE m.subs_id IS NOT NULL AND m.email_id IS NOT NULL AND m.email_id <> ''");
int res = query.executeUpdate();
System.out.println("Command successfully executed....");
System.out.println("Numer of records effected...,"+result.size());