How to simulate a Java object that contains instances of itself in a MySQL database?

I have a webapp that has an object that contains a collection of itself. I am struggling with how to model this in a MySQL database. This is a Task object that contains a list of subtasks. This is what the class looks like:

Task.java

public class Task{ 
    private int taskID;
    private String name;
    private String details;
    private List<Task> subTasks
    ...other properties

    public Task(int taskID, String name, String details){
        this.taskID = taskID;
        this.name = name;
        this.details = details;
        this.subTasks = new ArrayList<>();
    }

    Getters and Setters here...
}

After some research and thinking about it on my own, I came up with one possible solution. I have an image of a UML diagram, but I cannot post it since I just joined and my SO rep is not high enough yet. So I did my best to show it with the symbols below:

---------------------         -------------------
|       Task        |         |    SubTaskMap   |
--------------------- 1   ___|-----------------
| int taskID        |----|___|int subTaskID    |
| String taskName   |         |int parentTaskID |
| String taskDetail |         -------------------
--------------------           

, subTaskID parentTaskID SubTaskMap, taskID Task ( , Task Java). , , , parentTaskID , Task.

MS Access , parentTaskID, 5, Task.taskID SubTaskMap.subTaskID Task_Copy SubTaskMap, Task_copy.taskID SubTaskMap.parentTaskID SQL-, :

SELECT Task.taskName, SubTask.parentTaskID
FROM Task AS Task_Copy INNER JOIN (Task INNER JOIN SubTask ON Task.taskID = SubTask.subTaskID) ON Task_Copy.taskID = SubTask.parentTaskID
WHERE (((SubTask.parentTaskID)=5));

, ?

!

+4
3

, Composite pattern. : ?. Adjacency:

ParentTaskId SubTaskMap.

+2

, many-to-many . ( ).

, one-to-many. . , Task , . , , ORM.

, , ().

m-m - .

+1

, , parent_task_id. , .

If one task can have multiple parents, you will need another table to maintain this relationship.

0
source

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


All Articles