I'm relatively new to Spring and am hoping someone can help point me in the right direction here.
Imagine a to-do list app... Let's say I have a main entity called Task
and a task can have n
nested subtasks (each of which is a Task
and can have n
subtasks, etc.) I'm thinking that hte best way to represent this type of relationship is using a join table (see below for high level definition). The main issue here is circular references to the parent/child tasks. For example, if TaskA
has a subtask, TaskB
, and subtask TaskB
has a subtask, TaskA
.
I am wondering:
- Is there a way in spring boot / JPA to define some type of table validation or a SQL
CHECK CONSTRAINT
that will ensure that a set of task ids is not entered into the join table twice? E.g. in the above scenario an exception would be thrown b/cSet<TaskA, TaskB>
already exists in the table. - Is this the best approach to represent this type of object relationship? Is there a better way to do this with Spring Data / JPA given the requirements?
Thanks so much in advance!
@Entity
@Table(name = "task")
public class Task {
...
}
@Entity
@Table(name = "subtasks")
public class SubTasks {
...
private Task parentTask;
private Task childTask;
}