I have a hierarchical structure stored in a table. Each element has a pointer to its previous, next and a parent
create table CATALOGUE
(
NAME VARCHAR2(300) not null,
NEXT_ID NUMBER(38),
PARENT_ID NUMBER(38),
PREVIOUS_ID NUMBER(38),
XID NUMBER(38)
);
I have a java application, which uses O/R mapping to access and modify this table. Sometimes my catalog got corrupted, e.g. they are linked elements which don't have the same parent. I'm wondering if I can ensure data consistency using Oracle triggers or other pure SQL technique (without java code).
Is this "the correct way" of doing things?
How can I implement a trigger? I can implement a stored procedure which would validate my table. Something like
select count(*)
from catalogue c1, catalogue c2
where c1.next_id = c2.previous_id and c1.parent_id != c2.parent_id
should return 0.
But how can I call it on commit? I don't want to call it on every row update, just before the commit is finished with a possibility to rollback if my table is not valid.