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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:57:21+00:00 2026-06-18T10:57:21+00:00

I have the following relationships defined Entity A has @OneToMany with Entity B that

  • 0

I have the following relationships defined

Entity A has @OneToMany with Entity B that has @OneToOne with Entity C.

At the time of creation for Entity A, I also create Entity B and set the relationship between them. Everything works as expected.

In another transaction,
– Entity A is fetched from the DB, the corresponding Entity B is fetched and then
– Entity C is created.
– Entity C is set on Entity B to forge the relationship between the two.
If i use EntityManager.persist(Entity B), the persistence manager tries and inserts Entity B into the DB again causing a unique constraint violation on the PK of Entity B.
If i user EntityManager.merge(Entity B), everything works as desired.

In this above context of use, why would a persist operation try and create a duplicate since the Entity is managed in this case and it should easily be able to do an update.

  • 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-18T10:57:22+00:00Added an answer on June 18, 2026 at 10:57 am

    JPA requires providers to throw an exception if persist is called on an unmanaged instance. Only merge can be used since it is clear that it takes the data from the instance and merges it into a managed copy. If B were managed, the persist call would be ignored, so it means that some how B gets detached or that it was read in a different entityManager from the one you call persist on.

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

Sidebar

Related Questions

I have the following relationships set up: A HABTM B B belongsTo C C
Currently I have a database with the following relationships: One Client has multiple Entities
I need to create relationships for my entities, whereby I have the following scenario:
I have the following relationship CType has many Field Field has many Ctype and
I have the following domains: User, Role, Company. User and Role has m:n relationship,
Using the Entity Framework Code First paradigm I have defined the following objects and
I have the following relationship defined in Core Data Person --> Worked <-- Job
I have an entity Roster that has a collection of players. public class Roster
I have three entities: Country, State and City with the following relationships: When creating
I have the following relationship: Post hasMany Comment Post belongsTo Category Post HABTM Tag

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.