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

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

@Entity(name = "workflow_template")

public class WorkflowTemplate {

    
@Id
   
 @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 {

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

    
@ManyToOne
    @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.
Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

arnoldCommented:
Unfamiliar with what you reference, you could use a trigger that cascades
The data organization will be the guide.

Deals with avoiding orphan records with foreign keys, I.e. If you deleting a parent reference, the cascading trigger will delete the child accounts before the parent row can be deleted.

Depending on the data an actual deletion might not be wise, using a deleted column, or similar to make a row as deleted while it would kit be generally displayed, the info will be retained in the event the deletion was in error.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I'm not sure whether Hibernate has an inbuilt functionality, but depending on what database you are using, you may options there.

For instance, if you are using Oracle, when you create the foreign key relationship you can specify how it manages this scenario. One of these options is exactly what you want, it can delete any row that depends on the row being deleted.

Other DB's may/should have similar functionality.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.