Ho difficoltà a rimuovere child da un'associazione OneToMany. I miei soggetti:Rimozione di childs da @ OneToMany-association: CascadeType.ALL + orphanRemoval = true not working
@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;
...
mio infruttuoso tentativo di eliminare i Bambini:
personEntity.getProjects().clear();
questo funziona, ma non credo che questo è l'approccio giusto:
for (Iterator<ProjectEntity> iterator = personEntity.getProjects().iterator(); iterator.hasNext();)
{
ProjectEntity projectEntity = iterator.next();
projectDao.deleteEntity(projectEntity);
iterator.remove();
}
Che cosa sto facendo sbagliato qui?
Grazie
Jonny
Grazie JB Nizet, questo ha fatto il trucco. Qui il codice: 'for (Iterator iterator = personEntity.getProjects(). Iterator(); iterator.hasNext();) { ProjectEntity projectEntity = iterator.next(); projectEntity.setPerson (null); iterator.remove(); } ' –
user871611
Cosa succede se la persona non può essere nullo? –
@IgorG. Se la persona non può essere nulla, allora non ha senso rimuovere l'associazione. –