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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:02:53+00:00 2026-06-03T08:02:53+00:00

I have the situation where I have a class A having a OneToMany relationship

  • 0

I have the situation where I have a class A having a OneToMany relationship to class B. Class B only consists of a few attributes like id, firstName and lastName. The id is generated through UUID. Since it is very likely that very much instances of A will reference an instance of B with the same values for firstName and lastName plus those values in instances of B are very unlikely to ever change. So I decided to avoid duplicates and let reference instances of A to the same instances (in the database) of B if the other parameters are the same.

Example:

b: "John Doe"
a: a1 ("Foo Bar", "John Doe"), a2 ("John Doe", "Alan Smith").

To achieve this I always check the database for an already existing record with the very same parameters if a new B is added to A. If so, I’ll just overwrite the id of that instance of B with the one from the database and add it to A afterwards.

Now here comes my problem. Everything is working fine but if I persist an A where I manually changed the Id of an B so that it has the same Id as a B already existing in the database I’ll get an Exception:

Caused by: org.hibernate.exception.ConstraintViolationException:
Duplicate entry '2cfa1412-5f64-4a9c-b55c-f7db1148bef9' for key 'PRIMARY'

So my questions are:

  • How can I fix this problem without just ignoring the exception?
  • Is this a good way fitting my need?

Best regards

  • 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-03T08:02:54+00:00Added an answer on June 3, 2026 at 8:02 am

    First of all, you don’t have a OneToMany, but a ManyToMany.

    Now, to fix your problem, don’t assign the ID of the attached B to your detachedB, but replace the detached B by the attached one:

    Set<B> bs = new HashSet<B>(a.getBs()); // copy the list of Bs
    a.getBs().clear();
    for (B b : bs) {
        B existingB = searchBWithSameValuesAs(b);
        if (existingB != null) {
            a.getBs().add(existingB);
        }
        else {
            em.persist(b);
            a.getBs().add(b);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have situation like this: class IData { virtual void get() = 0; virtual
Pseudo-situation: have a class (let's say BackgroundMagic ), and it has Start() and Stop()
I have the following situation : abstract class X { abstract X someMethod (...)
I have the following manager<->worker situation: class Manager { private: pthread_attr_t workerSettings; pthread_t worker;
I have following situation - Have a MongoService class, which reads host, port, database
I have a situation where I have an instanced class that needs to call
We have this situation: Window Keyboard ^ ^ | / ApplicationWindow so class Window
I have following situation. In a constructor of a pseudo class I attach a
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have this situation: interface MessageListener { void onMessageReceipt(Message message); } class MessageReceiver {

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.