在Hibernate中使用HQL将数据插入到一个表中 [英] Inserting data in one table using HQL in Hibernate
问题描述
我正在阅读Hibernate HQL教程,发现HQL不支持 INSERT INTO..VALUES ..
但是 INSERT INTO..SELECT ..
即HQL仅支持从其他表插入。
假设我想在一个表中插入相同的值,并且该数据不是来自任何其他表,即该值不在任何其他表中。那么我该怎么做在HQL中?
另外,想知道HQL中这种限制背后的理性吗?
只需获取对您的实体的引用,即可获得一个持有Hibernate会话,并调用save()。
根据 http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch04.html#d0e2116
INSERT语句的伪语法
$ b
INSERT INTO EntityName properties_list select_statement
仅支持INSERT INTO ... SELECT ...表单。您无法指定要插入的显式值。
I was reading the Hibernate HQL tutorial and found that HQL doesn't support INSERT INTO..VALUES..
but INSERT INTO..SELECT..
i.e. HQL only support insert from another table.
Suppose I want to insert same values in one table and that data is not from any other table i.e. the values are not in any other table.Then how can I do that in HQL?
Also, would like to know the rational behind such restrictions in HQL?
You don't need to use hql to insert if the data is from another table.
Simply get a reference to your entity, get a hold of a Hibernate session, and call save().
According to http://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch04.html#d0e2116
Pseudo-syntax for INSERT statements
INSERT INTO EntityName properties_list select_statement
Only the INSERT INTO ... SELECT ... form is supported. You cannot specify explicit values to insert.
这篇关于在Hibernate中使用HQL将数据插入到一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!