Reputation: 1606
i am taking data from one table and want to insert into other table. First table containing many fields,i required only some fields out if that to store in new table. For that i am using the Hibernate query. I am able to fetch the result from query but when i try to insert into other table it throw
org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [insert into
Below is the code which throw exception.
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());
and when i run below code without using query.executeUpdate(); method
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();
Out put for above code is Command successfully executed.... Numer of records effected...,21.
Upvotes: 4
Views: 1975
Reputation: 5828
If you are doing an insert or an update you should only call
query.executeUpdate();
and not
query.list();
Try like this (you are missing the alias in the select part, so Hibernate is confused by 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());
Upvotes: 2