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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:57:59+00:00 2026-05-25T21:57:59+00:00

I have a @ManyToMany relationship between class A and class B : class A

  • 0

I have a @ManyToMany relationship between class A and class B : class A references a collection of class B instances, and this relationship is configured as CascadeType.ALL. So when a A is persisted with the entity manager, the B instances referenced by A are also persisted.

Both A and B have an ID declared with the GenerationType.IDENTITY strategy to use the auto_inc from the MySQL database.

The problem is :

  1. I create a new A
  2. I load some existing B objects from entityManager using JPQL
  3. I add the B objects to A’s collection of Bs

=> JPA tries to persist the B objects though they are already persisted (they just have been loaded).

I don’t understand why it tries to persist them again, they have been properly loaded, and their auto generated id has been well loaded as well.

The ManyToMany declaration :

@ManyToMany(cascade={CascadeType.ALL})
@JoinTable(name = "ENTRY_ENTITIES", joinColumns =
@JoinColumn(name = "ENTRY", referencedColumnName = "ID"),
inverseJoinColumns = @JoinColumn(name = "ENTITY", referencedColumnName = "ID"))
private List<Entity> entities;

The Query to load existing objects from database :

T result = (T) entityManager.createQuery("SELECT x FROM " + entityName + " x WHERE x.externalId='" + externalId + "'").getSingleResult();

The persisting :

UserTransaction transaction = getTransaction();
try {
    transaction.begin();
    entityManager.persist(entity);
    transaction.commit();
} catch (Throwable t) {
    Logger.getLogger(JpaDao.class.getName()).log(Level.SEVERE, null, t);
}

Thanks a lot for your help!

  • 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-25T21:58:00+00:00Added an answer on May 25, 2026 at 9:58 pm

    I’m not absolutely sure that this is the cause of your problem, but you should include the whole thing inside of a transaction. Not just the persistence part:

    start transaction
    load B from DB
    create new A
    add B to A
    commit transaction
    

    As I said in my comments, you have other design and coding problems:

    • CascadeType.ALL is wrong on a ManyToXxx association. You don’t want all the Bs of an A deleted when you delete A, since those Bs are referenced by other As. This will lead to constraint violations (in the best case) or an inconsistent database (in the worst case, if you have no constraint defined)
    • Don’t use string concatenation in your queries. Use parameterized queries. This will avoid quoting problems and injection attacks: SELECT x FROM A x WHERE x.externalId = :externalId
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ManyToMany relationship between two classes, for instance class Customer and class
I have an entity that has a ManyToMany relationship with itself: @Entity public class
I have an Account Entity with a @ManyToMany relationship to Role Entity. (This way
Simple JPA/JPQL question. I have an entity with a ManyToMany relationship: @Entity public class
I have this 2 classes: @Entity public class Student extends User { ... @ManyToMany(mappedBy
I have an Entity Account which has a ManyToMany Relationship to an Entity Role.
I have an entity class set up in Java, with a many-to-many relationship to
I have defined a many-to-many relationship between my two entity classes User and Permission.
I have a ManytoMany relationship between A and B, where A is the owning
I have a ManyToMany relationship between person that I'm trying to describe with Hibernate

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.