Announcement Announcement Module
Collapse
No announcement yet.
JpaRepository delete method remove all rows with the same manytomany relationship Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JpaRepository delete method remove all rows with the same manytomany relationship

    I have turned a part of the petclinic example to use spring data.
    I am learning and testing the Crud/JpaRepository methods. It is very good. Thanks to Spring Team.
    But I found a bug. The delete method is working fine as long the record hasn`t any objects of the many to many table. In the petclinic example we have a table called vet_specialties. So if the record in vets doesn`t have any references in vet_specialties the delete metod is fine. But if the vets record has a reference in vet_specialties we have a problem because the JpaRepository.delete(id) will remove all the records in the Vets table that has a similar reference.
    id first name last name Specialties
    1 James Carter
    2 Helen Leary radiology
    3 Linda Douglas surgery dentistry
    4 Rafael Ortega surgery
    5 Henry Stevens radiology
    6 Sharon Jenkins

    e.g. if JpaRepository.delete(2) then two records will be deleted row number 2 and row number 4.

    id first name last name Specialties
    1 James Carter
    3 Linda Douglas surgery dentistry
    5 Henry Stevens radiology
    6 Sharon Jenkins

    This is bug. only row number 2 should be deleted.

    My test code is like this

    @Autowired(required=true)
    private VetsRepository vetsRepository;
    @RequestMapping("/delete/vet")
    public ModelAndView createVetHandler(@RequestParam Long vetId) {
    vetsRepository.delete(vetId);
    ModelAndView mav = new ModelAndView("/vets");
    mav.addObject(new VetsList(Lists.newArrayList(vetsRepository.findAll ())));
    return mav;
    }
    The following code will work without deleting the reference in vet_specialties table
    Vets vet = vetsRepository.findOne(vetId);
    vet.setSpecialties(null);
    vetsRepository.delete(vet);

    So the problem is when a vets record has a list of spcialties. Then also order records in vets table with the same spcialties will be deleted.
    I hope the describtion is clear and it will help you to discover the bug. Either if it is a bug or not, please give some guide lines how to delete a record when it belongs to a ManyToMany relationship.
    Thanks

  • #2
    I doubt it is a bug and I think it is the result of the way the mapping is expressed in the entities (delete childs or something similair).

    Comment


    • #3
      If it has to do with the mapping please check my code
      Here is the mapping in the Vets class
      @Entity
      public class Vets {

      @Id @GeneratedValue private Long id;
      private String firstName;
      private String lastName;

      @ManyToMany(cascade=CascadeType.ALL)
      @JoinTable(name="vet_specialties",
      joinColumns={@JoinColumn(name="vet_id")},
      inverseJoinColumns={@JoinColumn(name="specialty_id ")})
      private List<Specialties> specialties;

      And the second class called specialties is mapped as this
      @Entity
      public class Specialties {
      @Id @GeneratedValue private Long id;
      private String name;
      @ManyToMany(cascade=CascadeType.ALL, mappedBy="specialties")
      private List<Vets> vets;
      Thanks
      Last edited by isys; Oct 25th, 2012, 10:34 AM.

      Comment


      • #4
        I found the problem.
        cascade=CascadeType.ALL shouldn`t be used in this case.
        When I removed cascade=CascadeType.ALL from the mapping it works perfect.
        Thanks for the hint

        Comment


        • #5
          Please use [ code][/code ] tags when posting code. I would remove, indeed, the CascadeType.ALL from the Specialties class, this will stop the removal of the referenced entities.

          Comment

          Working...
          X