move deletion of partnerRel from JPA Cascade to SQL after delete trigger

This commit is contained in:
Michael Hoennig 2024-03-28 10:00:50 +01:00
parent ca9a865019
commit 37c1886845
2 changed files with 16 additions and 10 deletions

View File

@ -17,7 +17,6 @@ import net.hostsharing.hsadminng.stringify.Stringifyable;
import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction; import org.hibernate.annotations.NotFoundAction;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
@ -28,6 +27,7 @@ import jakarta.persistence.Table;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import static jakarta.persistence.CascadeType.*;
import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Column.dependsOnColumn; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Column.dependsOnColumn;
import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Permission.*; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Permission.*;
import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Permission.SELECT; import static net.hostsharing.hsadminng.rbac.rbacdef.RbacView.Permission.SELECT;
@ -68,11 +68,11 @@ public class HsOfficePartnerEntity implements Stringifyable, HasUuid {
@Column(name = "partnernumber", columnDefinition = "numeric(5) not null") @Column(name = "partnernumber", columnDefinition = "numeric(5) not null")
private Integer partnerNumber; private Integer partnerNumber;
@ManyToOne(cascade = CascadeType.ALL) @ManyToOne(cascade = { PERSIST, MERGE, REFRESH, DETACH }, optional = false)
@JoinColumn(name = "partnerreluuid", nullable = false) @JoinColumn(name = "partnerreluuid", nullable = false)
private HsOfficeRelationEntity partnerRel; private HsOfficeRelationEntity partnerRel;
@ManyToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH }, optional = true) @ManyToOne(cascade = { PERSIST, MERGE, REFRESH, DETACH }, optional = true)
@JoinColumn(name = "detailsuuid") @JoinColumn(name = "detailsuuid")
@NotFound(action = NotFoundAction.IGNORE) @NotFound(action = NotFoundAction.IGNORE)
private HsOfficePartnerDetailsEntity details; private HsOfficePartnerDetailsEntity details;

View File

@ -33,21 +33,20 @@ create table hs_office_partner
( (
uuid uuid unique references RbacObject (uuid) initially deferred, uuid uuid unique references RbacObject (uuid) initially deferred,
partnerNumber numeric(5) unique not null, partnerNumber numeric(5) unique not null,
partnerRelUuid uuid not null references hs_office_relation(uuid), -- TODO: delete in after delete trigger partnerRelUuid uuid not null references hs_office_relation(uuid), -- deleted in after delete trigger
detailsUuid uuid not null references hs_office_partner_details(uuid) -- deleted in after delete trigger detailsUuid uuid not null references hs_office_partner_details(uuid) -- deleted in after delete trigger
); );
--// --//
-- ============================================================================ -- ============================================================================
--changeset hs-office-partner-DELETE-DETAILS-TRIGGER:1 endDelimiter:--// --changeset hs-office-partner-DELETE-DEPENDENTS-TRIGGER:1 endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
/** /**
Trigger function to delete related details of a partner to delete. Trigger function to delete related details of a partner to delete.
*/ */
create or replace function deleteHsOfficeDetailsOnPartnerDelete() create or replace function deleteHsOfficeDependentsOnPartnerDelete()
returns trigger returns trigger
language PLPGSQL language PLPGSQL
as $$ as $$
@ -59,17 +58,24 @@ begin
if counter = 0 then if counter = 0 then
raise exception 'partner details % could not be deleted', OLD.detailsUuid; raise exception 'partner details % could not be deleted', OLD.detailsUuid;
end if; end if;
DELETE FROM hs_office_relation r WHERE r.uuid = OLD.partnerRelUuid;
GET DIAGNOSTICS counter = ROW_COUNT;
if counter = 0 then
raise exception 'partner relation % could not be deleted', OLD.partnerRelUuid;
end if;
RETURN OLD; RETURN OLD;
end; $$; end; $$;
/** /**
Triggers deletion of related details of a partner to delete. Triggers deletion of related rows of a partner to delete.
*/ */
create trigger hs_office_partner_delete_details_trigger create trigger hs_office_partner_delete_dependents_trigger
after delete after delete
on hs_office_partner on hs_office_partner
for each row for each row
execute procedure deleteHsOfficeDetailsOnPartnerDelete(); execute procedure deleteHsOfficeDependentsOnPartnerDelete();
-- ============================================================================ -- ============================================================================
--changeset hs-office-partner-MAIN-TABLE-JOURNAL:1 endDelimiter:--// --changeset hs-office-partner-MAIN-TABLE-JOURNAL:1 endDelimiter:--//