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

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

i have the following two entities:

1- PlayList:

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


        
相关标签:
18条回答
  • 2020-11-30 00:47

    I also ran into this error on a badly designed database, where there was a Person table with a one2many relationship with a Code table and an Organization table with a one2many relationship with the same Code table. The Code could apply to both an Organization and Or a Person depending on situation. Both the Person object and the Organization object were set to Cascade=All delete orphans.

    What became of this overloaded use of the Code table however was that neither the Person nor the Organization could cascade delete because there was always another collection that had a reference to it. So no matter how it was deleted in the Java code out of whatever referencing collections or objects the delete would fail. The only way to get it to work was to delete it out of the collection I was trying to save then delete it out of the Code table directly then save the collection. That way there was no reference to it.

    0 讨论(0)
  • 2020-11-30 00:47

    Had the same error. Removing the object from the model did the trick.

    Code which shows the mistake:

    void update() {
        VBox vBox = mHboxEventSelection.getVboxSelectionRows();
    
        Session session = HibernateUtilEventsCreate.getSessionFactory().openSession();
        session.beginTransaction();
    
        HashMap<String, EventLink> existingEventLinks = new HashMap<>();
        for (EventLink eventLink : mEventProperty.getEventLinks()) {
            existingEventLinks.put(eventLink.getEvent().getName(), eventLink);
        }
    
        mEventProperty.setName(getName());
    
        for (Node node : vBox.getChildren()) {
    
            if (node instanceof HBox) {
                JFXComboBox<EventEntity> comboBoxEvents = (JFXComboBox<EventEntity>) ((HBox) node).getChildren().get(0);
                if (comboBoxEvents.getSelectionModel().getSelectedIndex() == -1) {
                    Log.w(TAG, "update: Invalid eventEntity collection");
                }
    
                EventEntity eventEntity = comboBoxEvents.getSelectionModel().getSelectedItem();
                Log.v(TAG, "update(" + mCostType + "): event-id=" + eventEntity.getId() + " - " + eventEntity.getName());
    
                String split = ((JFXTextField) (((HBox) node).getChildren().get(1))).getText();
                if (split.isEmpty()) {
                    split = "0";
                }
                if (existingEventLinks.containsKey(eventEntity.getName())) {
                    // event-link did exist
                    EventLink eventLink = existingEventLinks.get(eventEntity.getName());
                    eventLink.setSplit(Integer.parseInt(split));
                    session.update(eventLink);
                    existingEventLinks.remove(eventEntity.getName(), eventLink);
                } else {
                    // event-link is a new one, so create!
                    EventLink link1 = new EventLink();
    
                    link1.setProperty(mEventProperty);
                    link1.setEvent(eventEntity);
                    link1.setCreationTime(new Date(System.currentTimeMillis()));
                    link1.setSplit(Integer.parseInt(split));
    
                    eventEntity.getEventLinks().add(link1);
                    session.saveOrUpdate(eventEntity);
                }
    
            }
        }
    
        for (Map.Entry<String, EventLink> entry : existingEventLinks.entrySet()) {
            Log.i(TAG, "update: will delete link=" + entry.getKey());
            EventLink val = entry.getValue();
            mEventProperty.getEventLinks().remove(val); // <- remove from model
            session.delete(val);
        }
    
        session.saveOrUpdate(mEventProperty);
    
        session.getTransaction().commit();
        session.close();
    }
    
    0 讨论(0)
  • 2020-11-30 00:48

    I just to solve this problem.

    1. I set to null the value of list
    2. I saved the object (It worked)
    3. I set the new data to the object
    4. I resaved the object
    5. It works.!
    0 讨论(0)
  • 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<Vstan> vStanCollection;
    
    //  @LazyCollection(LazyCollectionOption.FALSE)
        @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "rju", orphanRemoval = true)
        private Collection<Underrju> 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<Underrju> underrjulist=underrju.getRju().getUnderRjuCollection();
            if(underrjulist.contains(underrju)) {
                underrjulist.remove(underrju);
            }
            underrjuDao.delete(id);
    
        }
         .........................
    }
    
    0 讨论(0)
  • 2020-11-30 00:50

    I had the same exception, caused when attempting to remove the kid from the person (Person - OneToMany - Kid). On Person side annotation:

    @OneToMany(fetch = FetchType.EAGER, orphanRemoval = true, ... cascade    = CascadeType.ALL)
    public Set<Kid> getKids() {    return kids; }
    

    On Kid side annotation:

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "person_id")
    public Person getPerson() {        return person;    }
    

    So solution was to remove cascade = CascadeType.ALL, just simple: @ManyToOne on the Kid class and it started to work as expected.

    0 讨论(0)
  • 2020-11-30 00:52
    cascade = { CascadeType.ALL }, fetch = FetchType.LAZY
    
    0 讨论(0)
提交回复
热议问题