我正在从一个表中提取数据,并希望将其插入到另一个表中。第一个表包含许多字段,我只需要其中的一些字段来存储在新表中。为此,我使用了Hibernate查询。我能够从查询中获取结果,但当我尝试插入到其他表时,它会抛出以下异常:org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [insert into]。下面是引发异常的代码。
以上代码的输出结果为: 命令执行成功... 受影响的记录数为 21。
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。