Jump to content

How to implement cascading deletion in my trigger in java


Recommended Posts


Iam trying to delete records related to the one that the user is trying to delete because the primary key this table is a foreign key in other tables and I want that when this record is deleted, the others or the key of it migrated are deleted. For this I placed a trigger but I cannot inject an object from the UserServiceEventContext interface knowing that my trigger method has as a parameter an object of type: BeforeDeleteOccurrenceContext. I tried with the three types of injection but it does not work

Table Value


public class ValeurEntity extends BaseEntity {



private String codeRepertoire;




private String codeValeur;



private String descriptionCourte;



Table ValueDescription


public class DescriptionValeurEntity extends BaseEntity {



private String codeRepertoire;



private Integer id;




private String idValeur;




private String codeValeur;




private String langue;



private String descriptionCourte;


And the DAO for entites

public abstract class BaseDAO extends BaseUtil {

private final Adaptation dataset;

private final Path tablePath;

private final UserServiceEventContext anEventContext;


protected BaseDAO(Adaptation dataset, UserServiceEventContext anEventContext) {

this.dataset = dataset;

this.anEventContext = anEventContext;

this.tablePath = this.setupTablePath();



public void delete(List items, boolean triggerActivation) {

Procedure deleteProcedure = (procedureContext) -> {





try {

Iterator var3 = items.iterator();


while(var3.hasNext()) {

T item = (BaseEntity)var3.next();

procedureContext.doDelete(item.getAdaptationName(), false);


} catch (Exception var5) {





UserServiceTransaction transaction = this.anEventContext.createTransaction();






I was thinking that i can use this delete method on the DAO to find all DescriptionValues related to the Value that we need to delete and delete them. But i cannot because in don't know how to inject a UserServiceEventContext in my trigger


I try your solution but it doesn't work:

public class ValueTrigger extends TableTrigger {



public void handleBeforeDelete(BeforeDeleteOccurrenceContext aContext)

throws OperationException {


Session session = aContext.getSession();

Adaptation record = aContext.getAdaptationOccurrence();

if(session.getAttribute("ROOT_OF_CASCADE_DELETE") == null){

session.setAttribute("ROOT_OF_CASCADE_DELETE", record.getOccurrencePrimaryKey());






Link to comment
Share on other sites

  • Create New...