deleted object would be re-saved by cascade (remove deleted object from associations)

后端 未结 18 1192
轮回少年
轮回少年 2020-11-30 00:19

i have the following two entities:

1- PlayList:

@OneToMany(fetch = FetchType.EAGER, mappedBy = \"playlist\", orphanRemoval = true, c         


        
18条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-30 00:49

    Another workaround

    I completely agree with redochka and Nikos Paraskevopoulos. Mahmoud Saleh's answer get over the issue in some circumstances not every time. In my situation I really need Eager fetchtype. So as Stony mentioned above I just removed from a list which cantian the object too. Here is my code:

    Rju entity

    public class Rju extends AbstractCompany implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 4294142403795421252L;
    
    
        //this field just duplicates @Column(name="callname") and serves for mapping to Rju fullname field
        @Column(name = "fullname")
        private String namerju;
        @NotEmpty
        @Column(name = "briefname")
        private String briefname;
    
        @LazyCollection(LazyCollectionOption.FALSE)
        @OneToMany(cascade = CascadeType.ALL, mappedBy = "otd")
        private Collection vStanCollection;
    
    //  @LazyCollection(LazyCollectionOption.FALSE)
        @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "rju", orphanRemoval = true)
        private Collection underRjuCollection;
    .........
    }
    

    Underrju entity

    public class Underrju extends AbstractCompany implements Serializable {
    
        /**
         * 
         */
        private static final long serialVersionUID = 2026147847398903848L;
    
    
        @Column(name = "name")
        private String name;
    
        @NotNull
        @Valid
        @JoinColumn(name = "id_rju", referencedColumnName = "id")
        @ManyToOne(optional = false)
        private Rju rju;
    ......getters and setters..........
    }
    

    and my UnderrjuService

    @Service("underrjuService")
    @Transactional
    public class UnderRjuServiceImpl implements UnderRjuService {
    
        @Autowired
        private UnderRjuDao underrjuDao;
    
        .............another methods........................
        @Override
        public void deleteUnderrjuById(int id) {
            Underrju underrju=underrjuDao.findById(id);
            Collection underrjulist=underrju.getRju().getUnderRjuCollection();
            if(underrjulist.contains(underrju)) {
                underrjulist.remove(underrju);
            }
            underrjuDao.delete(id);
    
        }
         .........................
    }
    

提交回复
热议问题