Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7059827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:16:44+00:00 2026-05-28T04:16:44+00:00

I am testing hibernate and giving this query to transaction = session.beginTransaction(); city =

  • 0

I am testing hibernate and giving this query to

transaction = session.beginTransaction();
city = new City("A");
city  = (City)session.merge(city);
city.setName("B");
transaction.commit();

And I am getting those queries in the command line:

Hibernate: insert into CITY (name) values (?)
Hibernate: update CITY set name=? where CITY_ID=?

I am using merge not save, so why hibernate is updating my object, It should not update. am it right? What is the mistake?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:16:45+00:00Added an answer on May 28, 2026 at 4:16 am

    I will try to explain using a more concrete example. Suppose you have a scenario like below :

    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    User userA = (User)session.get(User.class, 1101);
    transaction.commit();
    session.close();
    // Since session is closed, userA is detached.
    
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    User userB = (User)session.get(User.class, 1101);
    //Now here,  userB represents the same persistent row as userA.
    //When an attempt to reattach userA occurs, an exception is thrown
    session.update(userA);
    transaction.commit();
    session.close();
    

    Exception when an attempt to reattach a Detached object, userA is made.

    Exception in thread "main" org.hibernate.NonUniqueObjectException: a   
    different object with the same identifier value was already associated
    with the session:
    
    This is because Hibernate is enforcing that only a single instance of a Persistent    object exists in memory.
    

    To get around the above problem, merge() is used, as shown below :

    Session session = sessionFactory.openSession();
    Transaction transaction = session.beginTransaction();
    User userA = (User)session.get(User.class, 1101);
    transaction.commit();
    session.close();
    //userA is now detached as session is closed.
    
    session = sessionFactory.openSession();
    transaction = session.beginTransaction();
    User userB = (User)session.get(User.class, 1101);
    User userC = (User)session.merge(userA);
    if (userB == userC) {
      System.out.println("Reattched user is equal");
    }
    transaction.commit();
    session.close();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Hibernate 4.0.1.Final. I'm having trouble binding a session when testing my service.
I am testing Hibernate here is the situation and code: public static void main(String[]
I'm using DBUnit for testing DAOs implemented with Hibernate and Spring. I put a
I am testing with JUnit a PLSQL procedure , to his equals in Java/Hibernate.
I'm using Hibernate 4.0.1.Final to write a standalone Java app. When testing my data
Hi I'm testing some CRUD operations in Nhibernate. I have 3 tests in this
I am testing my Hibernate DAOs with Spring and JUnit. I would like each
I've got a hibernate-based application which uses DBUnit for unit testing. We have an
I am attempting to do unit testing of my DAO (using Spring and Hibernate).
I'm using H2 with Hibernate to generate in-memory DB on the fly for unit-testing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.