cascading on a target entity with other relationships

133 Views Asked by At

I have a manytomany relation mapped with these 3 entities :

@Entity
public class ApplicatifDo {
   .....
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "applicatifDo", fetch = FetchType.EAGER, orphanRemoval = true)
   private Set<ApplicatifTerminalDo> applicatifTerminalSet;
   .....
}

@Entity
public class ApplicatifTerminalDo {
   ......
   @ManyToOne
   @JoinColumn(name = "idApplicatif", nullable = false)
   private ApplicatifDo applicatifDo;
   @ManyToOne
   @JoinColumn(name = "idTerminal", nullable = false)
   private TerminalDo terminalDo;

   @Column
   private String remarques;
   ......
}

@Entity
public class TerminalDo {
   ......
   @OneToMany(cascade = CascadeType.ALL, mappedBy = "terminalDo", fetch = FetchType.EAGER, orphanRemoval = true)
   private Set<ApplicatifTerminalDo> applicatifTerminalSet;
   ......
}

I try to do many cascading tests on the join table ApplicatifTerminalDo from the two entities ApplicatifDo and TerminalDo. When I create or update a ApplicatifTerminalDo in the Set the cascading works well, but when it comes to orphanRemoval or delete It's not working

First :

For the delete I get the error :

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`softtwo`.`applicatifterminal`, CONSTRAINT `FK_295tcnx7wjuvv5se1g3vldxxn` FOREIGN KEY (`idApplicatif`) REFERENCES `applicatif` (`id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.Util.getInstance(Util.java:384)
.....

I would like that when I delete an entity ApplicatifDo or TerminalDo all rows related to them in the ApplicatifTerminal join table get deleted as well.

Second :

For the orphanRemoval, when I delete an element from the Set of ApplicatifTerminalSet in my ApplicatifDo entity and do a merge, then to test it I do a find by his Id of parent entity to get a new exact same entity and count the number of elements in the Set I get the good number (the number at the biginning with one less). But in my database I still have all my datas of my Set.

The code :

//My applicatifDo1 has 4 elements in the ApplicatifTerminalSet here

Assert.assertEquals(applicatifDo1.getApplicatifTerminalSet().size(), 4);
        Iterator<ApplicatifTerminalDo> iterator = applicatifDo1
                .getApplicatifTerminalSet().iterator();
        boolean first = true;
        while (iterator.hasNext()) {
            ApplicatifTerminalDo element = iterator.next();
            if (!first) {
                element.setRemarques("remarques updated");
            } else {

                    iterator.remove();



            first = false;
            }
        }

// updateApplicatifDo do just a merge
        applicatifDao.updateApplicatifDo(applicatifDo1.getId(), applicatifDo1);

            ApplicatifDo applicatifDo = applicatifDao
                    .findApplicatifDo(applicatifDo1.getId());

Assert.assertEquals(applicatifDo.getApplicatifTerminalSet().size(), 3);

When I do that the update of setRemarques() works well. I have no error in the console when I do that. Then the remove seems to work because I retrieve the same object by his Id it still says I have 3 elements, then the fourth has been deleted : BUT, when I look in phpmyadmin my 4 elements/relations in my appplicationterminal table are still there. If I do another TestNg later, just retrieving my applicatifDo by his Id this time it gets the four elements. Then there a big integrity problem here, and still I always use eagerly fetching. Any idea why such problem here ? And how could I make my cascading works ?

Third :

More globally, I have another cascading + orphanRemoval rules on other two entities and that works very well, but the entity target of these cascades does not have other relations with other entities. Clearly, there is specific rules (or maybe limitations) when cascading on entities with other relationships. Please do you know tutorials/rules explaining the best practices for such mappings ?

Thanks in advance. I'm stuck on this for too long.

0

There are 0 best solutions below