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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:51:14+00:00 2026-06-14T17:51:14+00:00

I have an Entity with a bidirectional ManyToMany relationship (on the same entity) marked

  • 0

I have an Entity with a bidirectional ManyToMany relationship (on the same entity) marked as CascadeType.ALL.

Here is how it looks for the Contact entity:

@ManyToMany(cascade= CascadeType.ALL)
private List<Contact> parentContacts = new ArrayList<Contact>();
@ManyToMany(cascade= CascadeType.ALL, mappedBy="parentContacts")
private List<Contact> childContacts = new ArrayList<Contact>();

I have a method on the server side supposed to do save this entity:

public AltContact saveContact(EntityManager em, Contact contact, List<Contact> childs) {
    for (Contact c : childs) {
        contact.getChildContacts().add(c);
        c.getParentContacts().add(contact);
    }
    if (contact.getId() == null) {
        em.persist(contact);
    } else {
        contact = em.merge(contact);
    }
    return contact;
}

It works great if the contact or any entities of the childs list are not persisted. But sometimes i will need to use the saveContact method with already persisted entities, and when i try to do this, it seems like JPA try to persist all the entities of the relationship and then will raise the exception:

Internal Exception: java.sql.SQLIntegrityConstraintViolationException

Indeed, it’ll try to repersist and already persisted entity, so the unicity of the ID field of Contact will be violated.

How can active this? What’s wrong with my approach?

How can i make Eclipselink/JPA does not try to persist entities that are part of the relationship and that are already persisted?

  • 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-14T17:51:16+00:00Added an answer on June 14, 2026 at 5:51 pm

    If your parent contact Entity is new but some of the children exist (but are detached), you do not want to call persist on the parent – this will cause the persist to cascade to the children which is required to cause an exception to be thrown either immediately or on the flush/commit. If you wish to call perist on the new parent contact, then you should try to find the existing children and associate the managed instances to the new contact. If it doesn’t exist, you can persist the child individually or allow persist to cascade over the relationship to do it for you.

    Generally though, it is much easier just to call merge on the new parent contact and have the provider figure out if it should be an update or an insert. Since merge cascades to the children, they too will be inserted or updated as appropriate.

    Be careful with the use of cascading though. When you go to remove a parent, it will also cascade the remove to the children, which then cascades to every parent in their parent collections. If it is not managed correctly, it could remove all contacts unintentionally. Also note that changes that cause a child to be removed from the list might not cascade correctly – cascade merge can only operate on entities that are currently in the collection, not ones that used to be there. So you may need to manually call merge on children when removing them from the parent’s collection or ensure they have merged called through a different path.

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

Sidebar

Related Questions

I have bi-directional relationship like this... Person.java public class Person{ @JsonIgnore @OneToMany(targetEntity=PersonOrganization.class, cascade=CascadeType.ALL, fetch=FetchType.EAGER,
I have an one-to-one, bidirectional entity relationship with shared keys. When I attempt to
I have the One-to-Many bidirectional relationship below. After generating the crud actions with a
If have an entity A with a bidirectional one-or-zero-to-one mapping with entity B. The
I have oneToMany bidirectional relationship between User and Address. In yml file, do I
I have OneToMany bidirectional relationship enity classes ( WorkOrder and Task ). WorkOrder have
I have entity Recipes and it has a HasMany collection comments. In a MVC
I have Entity Framework entities Events which have an EntityCollection of RSVP. I want
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I have entity e.g. Product which aggregates other entities such as Category . Those

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.