Here is my entities:
@Entity
@Table(name="workflows")
public class WorkflowDB {
...
@OneToMany(cascade = CascadeType.ALL, mappedBy="workflow", fetch = FetchType.EAGER)
private Set<ActivityDB> activities;
...
}
@Entity
@Table(name="activities")
public class ActivityDB {
...
@ManyToOne
@JoinColumn(name="id_workflow")
@Fetch(FetchMode.JOIN)
private WorkflowDB workflow;
@OneToMany(cascade = CascadeType.ALL)
@LazyCollection(LazyCollectionOption.FALSE)
@JoinTable(name = "dependency", joinColumns = {
@JoinColumn(name = "id_activity")}, inverseJoinColumns = {
@JoinColumn(name = "id_dependent")})
private Set<ActivityDB> dependencyActivities;
...
}
PS: Don't ask me why this strange (to me) @OneToMany. See here
Well, I have a Workflow. This Workflow have nodes ( activities ) wich are a graph ( nodes can have nodes to build a path )
Lets say I have a Workflow "W". So, "W".activities = {Ac1, Ac2} But, "Ac1".dependencyActivities = { Ac2 }
When I save the workflow by the first time, with some activities in "activities" Set, or when I update the workflow, all it's activities are duplicated in database, receiving new IDs.
The "activities" table will show:
-----------
! 1 ! Ac1 !
-----------
! 2 ! Ac2 !
-----------
! 3 ! Ac2 !
-----------
I supose Ac2 is duplicated because its in "W".activities set AND "Ac1".dependencyActivities set. Am I correct?
What can I do to avoid this behaviour? How to update the workflow AND it's children (and children of children recursively) at once?
****EDIT**** I think the point is how I see the dependency chain of the activities. All dependency solutions are wellcome.