Avatar of Rohit Bajaj
Rohit Bajaj
Flag for India

asked on 

How to delete rows with forign key while deleting only the top level row in hibernate

I am using hibernate for persistence.
Here are the two classes which i am using as entities :

@Entity(name = "workflow_template")

public class WorkflowTemplate {

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

    @OneToMany(cascade = {CascadeType.ALL})
@JoinColumn(name = "workflow_id")
@OrderColumn(name = "task_idx")
 private List<TaskTemplate> taskTemplates;

@Entity(name = "task_template")

public class TaskTemplate {

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

    @JoinColumn(name = "workflow_id", nullable = false)
private WorkflowTemplate workflowTemplate;

Open in new window

Now for deletion of the workFlowTemplate row i first need to delete the task template row and then delete the workflow_template row.
Is there a way where i could just specify delete the workflowTemplate row and it automatically deletes all the rest of such things which have a join in them.
DatabasesJavaJava EE

Avatar of undefined
Last Comment

8/22/2022 - Mon