Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
285 views
in Technique[技术] by (71.8m points)

java - Inserting data in one table using HQL in Hibernate

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...