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);
        }
WOlF
  • 115
  • 1
  • 8

1 Answers1

0

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);
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1st thx for ur reply I tried to manage the entities references as u showed it and i got this exception com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails Do u you think i should merge the new client statut before deleting the trader ? – WOlF Dec 09 '12 at 15:48
  • Just try adding `em.flush();` before the call to `em.remove(toDelete)`. – JB Nizet Dec 09 '12 at 15:53
  • same exception, may be i should clear the traderList before deleting him – WOlF Dec 09 '12 at 15:58
  • Show us the code of the two entities, and the method you're using (by editing your question). – JB Nizet Dec 09 '12 at 16:03
  • I finally resolved the problem: In addition to add the client to the ClientList of the new Trader(newAssignedTrader.getClients().add(c);) and setting it as the new trader (c.setTrader(newAssignedTrader);) i need to change the value of the foreign key wich references the 1st trader before removing it. (c.setIdTrader(newAssignedTrader.getIdTrader());) thx u very much =) – WOlF Dec 09 '12 at 16:51
  • This `idTrader` field in the client should not be there at all. It's completely redundant with the `idTrader2` association with the trader (which should be named `trader`, since it's not an ID, but a Trader instance). – JB Nizet Dec 09 '12 at 16:54