Usuwanie potomków z asocjacji @ OneToMany: CascadeType.ALL + orphanRemoval = true nie działa
Trudno mi usunąć dzieci ze stowarzyszenia OneToMany. Moje podmioty:
<code>@Entity @Table(name = "PERSON") public class PersonEntity extends BaseVersionEntity<Long> implements Comparable<PersonEntity> { ... // bi-directional many-to-one association to Project @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "person", orphanRemoval = true) private final Set<ProjectEntity> projects = new HashSet<ProjectEntity>(); ... @Entity @Table(name = "PROJECT") public class ProjectEntity extends BaseVersionEntity<ProjectPK> { @EmbeddedId private ProjectPK id; ... // bi-directional many-to-one association to UdbPerson @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "PERSON_ID", nullable = false, insertable = false, updatable = false) private PersonEntity person; ... @Embeddable public class ProjectPK implements Serializable { // default serial version id, required for serializable classes. private static final long serialVersionUID = 1L; @NotNull @Column(name = "PERSON_ID") private Long personId; ... </code>
Moja nieudana próba usunięcia dziecka:
<code>personEntity.getProjects().clear(); </code>
To działa, ale nie sądzę, że to właściwe podejście:
<code>for (Iterator<ProjectEntity> iterator = personEntity.getProjects().iterator(); iterator.hasNext();) { ProjectEntity projectEntity = iterator.next(); projectDao.deleteEntity(projectEntity); iterator.remove(); } </code>
Co ja tu robię źle?
Dzięki
Jonny