Shubham Sangam
Shubham Sangam

Reputation: 3

Can we make spring entity not delete able by any user?

I'm using postgres with my spring boot app and I want to make a specific table not delete able by anyone. Is there a way to implement this on spring boot side.

Revoking delete grant does the job on postgres side but I want to configure entity such that it won't be delete able by anyone.

Upvotes: 0

Views: 53

Answers (2)

stylepatrick
stylepatrick

Reputation: 408

If you use JPA you could overload the delete methode from the Repository. None is then able to delete entries over it.

Upvotes: 0

Bohemian
Bohemian

Reputation: 425208

Create a trigger on the table to prevent deletion:

create function prevent_deletion()
returns trigger as '
begin
    raise exception ''deletion not allowed'';
end;
' language plpgsql;


create trigger prevent_deletion_trigger 
before delete on mytable
for each row 
execute function prevent_deletion();

See live demo.

Upvotes: 1

Related Questions