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 1013439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:06:07+00:00 2026-05-16T10:06:07+00:00

i have an entity that contains two other entities with @ManyToOne relationship. @Entity public

  • 0

i have an entity that contains two other entities with @ManyToOne relationship.

@Entity
public class A extends Serializable{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    @Cascade(CascadeType.SAVE_UPDATE)
    private B b;

    @ManyToOne
    @Cascade(CascadeType.SAVE_UPDATE)
    private C c;

}

If i try to save an A instance that have “B_ID” and “C_ID” of another A record i get the exception:

org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session

For example:

A table
|   ID  |   B_ID    |   C_ID    |
|    1  |      1    |   null    |    // this works
|    2  |   null    |      1    |    // this works
|    3  |      1    |      x    |    // this throws the exception
|    4  |      x    |      1    |    // this throws the exception

x=any value of existent B/C_ID 

B_ID and C_ID are not unique in my model and (B_ID + C_ID) is not an unique constraint!!

What can i do?

Thank in advance.

  • 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-16T10:06:08+00:00Added an answer on May 16, 2026 at 10:06 am

    Hibernate is not complaining about database uniqueness here, it’s complaining that the current Session already contains an object with the same ID as a new object that you’re trying to save. It won’t permit this – Hibernate has a strict requirement that a given ID cannot be represented by two different objects in the scope of a single session.

    At some point, your application has save dor loaded an entity with that same ID, and that object is already “registered” with the session. It’s hard to tell in this specific case which ID it’s complaining about, since the exception text isn’t clear. Try temporarily removing the cascade directives and see if it still happens, try to it narrow down.

    If necessary, you can force the session to “forget” about any existing objects for a given ID (using Session.evict() in the Hibernate API, or EntityManager.detach() in the JPA 2.0 API), but that’s not a very elegant solution.

    To reiterate – this exception has nothing at all to do with the database constraints, it’s about Hibernate protecting the consistency of its internal in-memory state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity that contains a list of other entities. The list of
in an app i have an entity that contains a list of other entities
Consider that i have two entities with following relationship: Entity A <-->> Entity B
I have two Doctrine entities that have a one-to-many relationship, like this: License class
I have an entity class Foo foo that contains Collection<Bar> bars . I've tried
Imagine you have an entity that has some relations with other entities and you
I've got two entities with a one-to-many relationship between them. The entity that holds
I have two entities, User and UserPermission. The User entity contains all your normal
I have a User entity that contains the username, name and other non sensitive
I have a simple XCDataModel that contains one Entity with One Attribute. Essentially, I

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.