如何编写HQL插入查询? [英] How to write HQL Insert query?
问题描述
我正在努力编写用于在表中插入新记录的HQL查询.我已经看到了一些插入查询,如下所示,但我不想从下面的代码中插入另一个表中的数据.
I am struggling to write a HQL query for inserting a new record in a table. I have seen some of insert query as below but I don't want insert data from another table as below code.
String hql = "INSERT INTO Employee(firstName, lastName, salary)" +
"SELECT firstName, lastName, salary FROM old_employee";
Query query = session.createQuery(hql);
int result = query.executeUpdate();
System.out.println("Rows affected: " + result);
例如,我有一个表"User",其中有三个字段,例如名称,年龄,数字,并且我有此用户表的实体.插入查询将是什么?
For example I have a table "User" with three fields like name, age, number and I have entity for this user table. what will be the insert query for this?
推荐答案
在HQL中,仅支持INSERT INTO…SELECT….没有INSERT INTO…VALUES. HQL仅支持从另一个表插入.
In HQL, only the INSERT INTO … SELECT … is supported; there is no INSERT INTO … VALUES. HQL only support insert from another table.
这样可以从SELECT查询INSERT
So query INSERT from SELECT is possible like this
Query query = session.createQuery("insert into Stock(stock_code, stock_name)" +
"select stock_code, stock_name from backup_stock");
int result = query.executeUpdate();
如果您有值和实体,请致电
If you have values and Entity just call
MyEntity e=new MyEntity();
e.setXXXX(the XXX values);
save(e);
这篇关于如何编写HQL插入查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!