i have the following two entities:
1- PlayList:
@OneToMany(fetch = FetchType.EAGER, mappedBy = \"playlist\", orphanRemoval = true, c
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.
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();
}
I just to solve this problem.
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);
}
.........................
}
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.
cascade = { CascadeType.ALL }, fetch = FetchType.LAZY