Get all updates of Java4s
Follow Us: Email Newsletter

Part 7 Hibernate Query Language Insert Query

Hibernate » On Jul 10, 2011 { 11 Comments } By Sivateja

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 Author ♦

Full Stack( Java/J2EE & UI/UX ) Developer and founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog. Sign-up for the Email Newsletter for your daily dose of Java tutorials.


11 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. MR Chowdari says:

    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??


By posting your answer, you agree to our comments policy.
What would you like to learn today?
Browse these popular technologies
Hibernate spring struts Servlets ajax Log4j jQuery Json Core Java Web Services AngularJs
Most Recent Tutorials
Hibernate_Logo Hibernate Recent Posts Spring_Logo Spring Recent Posts Struts_Logo Struts Recent Posts
Useful Links Current & UpComing Tutorials
  Web Services [ SOAP ], JSP
Contact | About Us | Privacy Policy | Advertise With Us

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