【问题标题】:org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [insert intoorg.hibernate.hql.internal.QueryExecutionRequestException:不支持 DML 操作 [插入
【发布时间】:2016-07-28 07:46:58
【问题描述】:

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

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

下面是抛出异常的代码。

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。

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    如果您正在执行插入或更新,您应该只调用

    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());
    

    【讨论】:

    • 我删除了 query.list();并再次运行程序。但问题仍然存在。
    • 我尝试了上面的代码,它抛出异常 org.hibernate.hql.internal.ast.QuerySyntaxException:期待 OPEN,在第 1 行第 33 列附近找到“a”[插入
    猜你喜欢
    • 2019-05-03
    • 2018-07-23
    • 2019-02-01
    • 1970-01-01
    • 2021-09-28
    • 2013-07-11
    • 2015-10-08
    • 1970-01-01
    • 2015-11-27
    相关资源
    最近更新 更多