I'm having trouble deleting a child of a one-to-many relationship object. Here's the code snippet:
@Override
@Transactional
public void deleteTask(UUID listId, UUID taskId) {
TaskList list = repo.findOne(listId);
System.out.println("Old List: " + list);
for(Task t : list.getTasks()) {
if(t.getId().toString().equals(taskId.toString())) {
System.out.println(list.getTasks().remove(t));
System.out.println("Task with id " + taskId + " deleted.");
}
}
System.out.println("New List: " + repo.save(list));
}
Task Class:
@Entity(name = "task")
public class Task implements Serializable {
@ManyToOne
@JoinColumn(name="tasklist_id")
private TaskList parentList;
public Task() {}
}
and class TaskList:
@Entity(name = "task_list")
public class TaskList implements Serializable {
@OneToMany(mappedBy="parentList", cascade={CascadeType.ALL})
private List<Task> tasks;
public TaskList() {}
}
The object Task
is a child, and even if the save () function returns truncated TaskList
, I can’t get the changes that will be displayed in a separate database query. The number of tasks remains unchanged. However, deleting a list with the help repo.delete(listId)
works fine with both the list and its tasks.
Here repo
is the repository corresponding to the parent class TaskList
. All operations with a child class Task
occur through a relation @OneToMany({cascade=CascadeType.ALL})
.
- TaskList
repo.findAll()
.
, , - . , , .