Get all updates of Java4s

Example On Hibernate Update Query

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


20 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(

    • Dhirendra Kumar says:

      if you use oracle 10g database then use this dialect

      <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>

      or choose specific database dialect

  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.

  14. amit yadav says:

    Thanks in Advance..

    In second approch(Explicitly call update()) only one time my values are changing in db,

    But when i am try to update second time ,i am getting lock Exception that its saying it is try to update or delete by other transaction.

    • Sheelpriya says:

      Hi Amit,

      According to my analysis, u have followed second approch ,new object is created and set the value and call update but on second u should create the new object and set the value and call the update and commit the have done the same thing or another?

  15. Neeraj says:

    If we call update method by creating object with already save id but object with same id not found during update call then will it make a new object with same id or some thing else

  16. naresh says:

    Superb man excellent java teaching style never ever will be seen in any blog or website. My whole-hearted thanks to you and your team

  17. Kalidas says:

    Hi Sir,

    Related to approach 2:
    If you are updating 2 properties only then another properties value is 0(default) value store into DB.
    See below example :
    Product p=new Product();
    p.setProductId(104); // 104 must be in the DB
    // Price = 0 i,e default(int/double) — if you are not p.setPrice() then update method set 0 in Price column
    // String store null
    @Java4s — In above Example Please Update Price i.e 0 by default

    Kalidas Daundkar


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.