Get all updates of Java4s
Follow Us: Email Newsletter

Example On Hibernate Update Query

Hibernate » On Jun 13, 2011 { 13 Comments } By Sivateja

This is the program to update an object (1 complete row) in the database, which is already persisted in the database, then we have the following two approaches…

Approach 1

Load that object from the database, and modify its values, now hibernate automatically modifies the values on to database also, when ever the transaction is committed.

Approach 2:

If we want to modify object in the database, then create new object with same id and we must call update() given by session interface.

Files required to execute this program..

  • (My POJO class)
  • product.hbm.xml (Xml mapping file )
  • hibernate.cfg.xml (Xml configuration file)
  • file to write our hibernate logic)

Related to approach 1: (POJO)



hibernate.cfg.xml (* Recommended way)


  • See line number 20, actually there i tried to update Stno(105), we should not do this, because we have loaded the object from the database with his id number only, see line number 16, if we update hibernate will rises the exception
  • See line number 24 once we call the commit(), automatically update method will be called by hibernate.
  • When ever an object is loaded from the database then hibernate stores the loaded object in cache-memory maintained by session-interface
  • Once an object is loaded, if we do any modifications on that object by calling its setter methods, then these modification are stored in the object maintained by cache-memory
  • if we modify the loaded object for multiple times then also the modifications will be stored in object maintained by the cache-memory only.
  • when ever we issue commit() operation then hibernate verify whether any changes are there between the object stored in the cache and object in the database, if changes exists then hibernate automatically updates the database by generating any update operation.
  • What am saying is hibernate automatically maintains synchronization between cache-memory object and database table objects (rows)

Related to approach 2:


Oracle DB, Before Run The Program

Oracle DB, After Run the program


  • From line number 17 to 19, we created new object and modified, and in line number 22 we are calling update method explicitly
  • Here we no need to load an object from the database
  • we will create a new object, and we will assign same id no’s to it and we will call update() explicitly in order to make the changes on the object that is stored in the database

That’s it, actually first approach is  recommended always..   :-)

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.


13 Responses to “Example On Hibernate Update Query”
  1. hi Java4s team , is very very best website for java developer. i have clear so many Java related doubts using this site .
    thanks and regard.
    Raju Kapadne

  2. Ganesh says:

    In (* Recommended way) , the line number 21 you commented s.setStno(105);
    In Product class there is no setStno() method. its setProductId() method right !
    Sorry. i am always commenting to show the mistakes .
    My intention is just to correct … so others can get more benefit from this tutorials …
    Hope you can take it positively …
    thank you…

  3. Java4s says:


    Thanks for the update :-) no problem, i am really very happy to hear.
    While doing the application i have changed the method, no issues just change the method and carry on.

  4. Raghu says:

    in the second approach when i update price column also changing with value ZERO, may I know why than happen ,
    Thanks in advance

  5. What is synchronization in database please clear this topic.
    Thanks in Advance

  6. aspnetcs says:

    No row with the given identifier exists: [str.Product#105]

  7. This site is more useful.Thanks.

  8. Parveen Thakur says:

    Hi All,
    I want to know that we need to write
    Because when I am not using the above statement , I got updated object.

  9. Varan says:

    i got the problem in updating table
    i am using oralce11g
    i got the followng exception how to solve it plz help me
    Exception in thread “main” org.hibernate.HibernateException: Hibernate Dialect must be explicitly set at org.hibernate.dialect.DialectFactory.determineDialect(
    at org.hibernate.dialect.DialectFactory.buildDialect(
    at org.hibernate.cfg.SettingsFactory.determineDialect(
    at org.hibernate.cfg.SettingsFactory.buildSettings(
    at org.hibernate.cfg.Configuration.buildSettings(
    at org.hibernate.cfg.Configuration.buildSessionFactory(
    at hib.varan.UpdateClientLogic.main(

  10. krishna says:


    Use oracleDialect

  11. Pallavi says:

    Hi ,

    I am getting following error:

    “Exception in thread “main” org.hibernate.QueryException: Expected positional parameter count: 1, actual parameters: [] [from Product p where = ?]” at the position where I query the database.

    I am not able to figure out why.

    Could you please tell me where I am going wrong.

  12. Venkat says:

    I have a doubt. Is it really necessary to provide configuration file name at the time of configuration.
    Configuration cfg = new Configuration();

    I am able to run my program even if i did not gave the Configuration file name.

  13. Naga says:

    Can you please tell me how to do soft deletion in table using hibernate.Using if condition.

    we have to create entity class object and then we will get that entity reference then what is the process please help me i will strucked there.


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 - 2016 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.