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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:42:00+00:00 2026-06-15T17:42:00+00:00

Here is the situation : I have 2 entities Trader and client : –

  • 0

Here is the situation :
I have 2 entities Trader and client :
– one client is associated to one trader
– one trader could be associated to one or many clients
while deleting a trader using EntityManager.remove(t) then commit, all the clients releated to that trader are deleted automatically. it’s fine because it’s cascade deleting.
Question :

1 – How can i remove my trader and reaffect its client to an other one ?

2 – Does the solution resides in desabling the cascade effect in mySQL or it resides in both entityManager and MySQL ?
thx u very much for any help.

Client Entity

public class Client implements Serializable {
    @Transient
    private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id_client")
    private Integer idClient;
    @Column(name = "id_portefeuille")
    private Integer idPortefeuille;
    @Column(name = "id_entreprise")
    private Integer idEntreprise;
    @Column(name = "id_trader")
    private Integer idTrader;
    //other attributes 
    @JoinColumn(name = "id_trader", referencedColumnName = "id_trader", updatable = false, insertable = false)
    @ManyToOne
    private Trader idTrader2;
    //getters and setters 

Trader Entity

public class Trader implements Serializable {
        @Transient
        private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
        private static final long serialVersionUID = 1L;
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
        @Column(name = "id_trader")
        private Integer idTrader;
        @Column(name = "nom")
        //other attributes
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "idTrader2")
        private Collection<Client> clientCollection;
        //getters and setters 

Main code

//Getting The trader selected in a JTable        
 Trader t = traderList.get(tableTrader.getSelectedRow());

 for (Iterator<Client> it = t.getClientCollection().iterator(); it.hasNext(); ) {

           //Getting the Clients releated to this trader
            Client c = it.next();

           //Affecting the client to an other trader 
           // i chose an existing one randomly 
            c.setIdTrader2(traderList.get(2));
           traderList.get(2).getClientCollection().add(c);

           it.remove();
    }
            entityManager.flush();
            entityManager.remove(t);
        }
  • 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-15T17:42:01+00:00Added an answer on June 15, 2026 at 5:42 pm

    There should be no delete in cascade configured, neither in MySQL not in the entity mapping. The method would thus look like this (assuming there is a bidirectional association, and thet getClients() returns the persistent list directly, without making a defensive copy or wrapping it into an unmodified proxy):

    public void deletedTrader(Trader toDelete, Trader newAssignedTrader) {
        for (Iterator<Client> it = toDelete.getClients().iterator(); it.hasNext(); ) {
            Client c = it.next();
            c.setTrader(newAssignedTrader);
            newAssignedTrader.getClients().add(c);
            it.remove();
        }
        em.remove(toDelete);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the situation: I have multiple clients and one host. The message format&size
Here is my situation: I have one table that contains a list of drugs
Here is my situation: I have a C project linking with many libraries (I
Here's the situation I have a webpage which has one drop down called prefer.
Here is the situation: You have one long-running calculation running in a background thread.
Here's the situation: We have some generic graphics code that we use for one
Here is the situation, i have three entities of which has the following relationships:
Here is the situation. I have 3 tables, one super type, and two sub
Here's my situation.. Suppose you have the following model entities, which represent single tables
Here is the situation : we have to offer a customer with a web-based

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.