Get all updates of Java4s

Part 7 Hibernate Query Language Insert Query

Now we will see how to use HQL insert query, as i told earlier its little different then remaining query’s, actually the thing is…..

HQL supports only the INSERT INTO……… SELECT……… ; there is no chance to write INSERT INTO………..VALUES, i mean while writing the insert query, we need to select values from other table, we can’t insert our own values manually, you will understand by seeing the following example..

files required…

  • (Our POJO class)
  • Product.hbm.xml
  • (Our POJO class)
  • Items.hbm.xml
  • hibernate.cfg.xml
  • (For Our logic)




  • see – line number 17, 18 that’s the insert query of HQL, while inserting the values into Product table, it will takes values from Items table
  • for executing these DML operations we need to call executeUpdate() method, and it will returns how many records effected due query execution (returns one integer value)


Database Output Before Execution [PRODUCTS table]

Database Output Before Execution [ITEMS table]

Eclipse Console after execution


Database output after execution

You Might Also Like

  ::. About the Author .::

- Full Stack Java/J2EE & UI Web Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.


16 Responses to “Part 7 Hibernate Query Language Insert Query”
  1. chinnasamy says:

    In this tutorial you don’t mention, how to configure two mapping file to configuration file.

  2. anusha says:

    how to configure two mapping files in hibernate.cfg.xml?

  3. Hi Anusha,

    Please look over this for config file



  4. chetan says:

    while running this program i got my table unmapped why?????

  5. rashid says:

    here you are inserting one table values to another table what if i want add a record into a table from user

  6. Nitya says:

    Hi Sivateja,as you mentioned above while inserting record ,it will take from one table and insert into another another table…why this is hapenning,if there is no data in one table then how it will fetch and insert into another table…and if we enter from UI then how the mapping wil happen…

  7. Revathi says:

    How to write a query if I want to insert my own values instead of retrieving values from some other table?

  8. karunakar kukunoori says:

    hai ,teja i also have same doubt why cant we insert direct values through hql when we are performing select,delete,update operation

  9. palani says:

    sir can you post the configuration file for this two hbm file getting mapped

  10. Palani says:

    sir please post the configuration file for this project….. i wasted two days

  11. Devkaran Singh Rathore says:

    You haven't created object of Item CLASS ,still u are using it in query .how??

  12. Aakash Deep Sharma says:

    Sir same query . how to insert data in Item table ?
    can we insert our own values rather than from different Table ?

  13. mousita says:

    Caused by: line 1:1: unexpected token: INSERT
    the above error is coming…pls help

  14. Narasingha says:

    Query qry = session.createQuery("insert into Product(productId,proName,price)
    select i.itemId,i.itemName,i.itemPrice from Items i where i.itemId= ?");

    Are we not setting the values for productId,proName,price ?

  15. Shweta says:

    The code is running but changes are not reflected. The data present in items was not copied in products.


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Spring-Boot-tutorials Hibernate tutorials Spring tutorials Struts tutorials Servlets tutorials Ajax tutorials Log4j tutorials jQuery tutorials Json tutorials Core Java tutorials Web Services tutorials AngularJs tutorials
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2018 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.