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

  • SEARCH
  • Home
  • 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 8496731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:56:03+00:00 2026-06-10T23:56:03+00:00

Possible Duplicate: Spring + Hibernate : a different object with the same identifier value

  • 0

Possible Duplicate:
Spring + Hibernate : a different object with the same identifier value was already associated with the session

I have the following object (simplified) as a JavaBean:

public class Person {
  private Integer id;
  private City cityOfBirth;
  private City city;
  // ...
}

And in my spring form I have 2 select combos in order to choose both cities, as follows:

<form:form method="post" action="" commandName="person">
City of birth: <form:select path="cityOfBirth" items="${ cities }" itemValue="id" />
City: <form:select path="city" items="${ cities }" itemValue="id" />
...
</form:form>

My PropertyEditor for the City class would simply call to my CityDao’s get, wich is the following:

@Component
public class CityDaoImpl implements CityDao {
  private @Autowired HibernateTemplate hibernateTemplate;

  public City get (Integer id) {
    return (City) hibernateTemplate.get(City.class, id);
  }
}

And my PersonDao would do this in order to save the instance:

public void save (Person person) {
  hibernateTemplate.saveOrUpdate (person);
}

Everything works OK when I try to save a Person with 2 different cities, but when I choose the same City I obtain the following error:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.project.si.models.common.City#90]

I’ve read in other posts that this happens because Hibernate Session is currently aware of the previous City obtained when the propertyEditor called cityDao.get(id), so I’m supposed to use merge() somewhere, but I don’t get where should I apply this..

  • 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-06-10T23:56:05+00:00Added an answer on June 10, 2026 at 11:56 pm

    The problem is caused by the fact that you cascade saveOrUpdate() operation from Person to City, and update() fails when object with the same id as the object to be updated is already associated with the Session.

    I think the best solution for this problem is to remove cascading options from city and cityOfBirth.

    As long as your user have to select a City from a list of existing cities, cascading doesn’t make sense here because you don’t need to cascade any changes from Person to City.

    Alternatively you can use merge() instead of saveOrUpdate():

    public void save (Person person) {
        hibernateTemplate.merge(person); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Hibernate: different object with the same identifier value was already associated with
Possible Duplicate: Spring + Hibernate : a different object with the same identifier value
Possible Duplicate: Spring + Hibernate : a different object with the same identifier value
Possible Duplicate: How to implement AOP with Spring i am using spring for hibernate
Possible Duplicate: (String) or .toString()? I have an Object. Is it better to do
Possible Duplicate: String with array structure to Array I have an array as such
Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =
Possible Duplicate: New to Spring - BeanFactory vs ApplicationContext? Why we always use the
Possible Duplicate: determine target url based on roles in spring security 3.1 Hello all
Possible Duplicate: What is dependency injection? Spring is the framework from where the concept

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.