Spring JPA - deleting a child is not reflected in the database table

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 {    

    // Id and 3 fields

    @ManyToOne
    @JoinColumn(name="tasklist_id")
    private TaskList parentList;

    // 3 more fields

    // Constructor
    public Task() {}

    //Getters and Setters
}

and class TaskList:

@Entity(name = "task_list")
public class TaskList implements Serializable {

    // Id and two fields

    @OneToMany(mappedBy="parentList", cascade={CascadeType.ALL})
    private List<Task> tasks;

    // Constructor
    public TaskList() {}
}

The object Taskis 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 repois the repository corresponding to the parent class TaskList. All operations with a child class Taskoccur through a relation @OneToMany({cascade=CascadeType.ALL}).

- TaskList repo.findAll() .

, , - . , , .

+4
2

orphanRemoval = true :

@OneToMany(mappedBy="parentList", cascade={CascadeType.ALL}, orphanRemoval=true)

list.getTasks().remove(t) , JPA, . orphanRemoval.

+4

@Entity(name = "TASK_LIST")
public class TaskList {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@Column(name = "NAME")
private String name;

@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "task", referencedColumnName = "id", nullable = false)
private List<Task> tasks = new ArrayList<Task>();

@Repository
public interface TaskListRepository extends JpaRepository<TaskList, Long> {
}

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext-test.xml")
public class TaskListRepositoryTest {

    @Autowired
    private TaskListRepository repository;

    @Autowired
    private TaskService service;

    @Test
    public void test1() throws SQLException {

        TaskList taskList = new TaskList();
        taskList.getTasks().add(makeTask("name1", "description1"));
        taskList.getTasks().add(makeTask("name2", "description2"));
        taskList.getTasks().add(makeTask("name3", "description3"));
        taskList.getTasks().add(makeTask("name4", "description4"));
        taskList.getTasks().add(makeTask("name5", "description5"));

        service.save(taskList);

        TaskList findOne = repository.findOne(1l);
        assertEquals(5, findOne.getTasks().size());

        taskList.getTasks().remove(2);
        service.save(taskList);

        findOne = repository.findOne(1l);
        assertEquals(4, findOne.getTasks().size());
    }

    @Test
    public void test2() throws SQLException {

        TaskList findOne = repository.findOne(1l);
        assertEquals(4, findOne.getTasks().size());
    }

    private Task makeTask(String name, String description) {

        Task task = new Task();
        task.setName(name);
        task.setDescription(description);
        return task;
    }
0

Source: https://habr.com/ru/post/1627745/


All Articles