0

When I am deleting a user from the users table, all of his posts and any comments to this posts should be deleted as well.

The model looks like the following:

@Data
@Entity @Table(name = "users")
public class BlogUser {

    @Id
    private String userName;
    private String password;
    private String email;

    @Enumerated(EnumType.STRING)
    private Role role;

    private boolean enabled;
}

post instance has a reference to belonging user:

@Data
@Entity @Table(name = "posts")
public class Post {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String postText;

    @ManyToOne(cascade = CascadeType.ALL)
    private BlogUser user;

    private LocalDateTime createdDate;
}

The same situation applies for comments.

Now when I want to perform a delete I get this error:

org.postgresql.util.PSQLException: ERROR: update or delete on table "users" violates foreign key constraint "fkqdk379brhxkbj4c8qenbuu85l" on table "posts"

DB is Postgres. I tried to use @ManyToOne(cascade = CascadeType.ALL), but it didn't help.

UPDATE:

The idea is that I want to keep current schema for tables.
Without adding posts and/or comments to BlogUser class.

2
  • On hibernate side it seems to be ok, is your database schema generated from your hibernate classes or the other way around ? Or both created manually ?
    – Tapaka
    Jan 26, 2018 at 16:47
  • @Tapaka DB is created manually. And all tables generated by application. I am using spring boot app.
    – catch23
    Jan 26, 2018 at 16:57

2 Answers 2

2

You need to add cascade to BlogUser side also in order to have delete on it to be cascaded to Post & Comment.

Add something like this to your BlogUser class:

@Getter
@OneToMany(cascade=CascadeType.ALL, mappedBy="user")
private Collection<Post> posts = new HashSet<>();

@Getter
@OneToMany(cascade=CascadeType.ALL, mappedBy="user")
private Collection<Comment> comments = new HashSet<>();
2
  • 1
    For the update: in JAVA side then the possibilities are limited. My answer should not touch schema. And if you do not want to do as Tapaka suggested then you can always fetch all posts / comments by user and delete them before deleting user.
    – pirho
    Jan 29, 2018 at 15:10
  • I come up with the same solution today. And make all deletion of those data transactional.
    – catch23
    Jan 29, 2018 at 16:24
2

Your problem most likely comes from the fact that you don't have the ON CASCADE declared on your tables. Basically you need to drop your id's constraint between users / posts & posts / comments and recreate them adding ON CASDADE DELETE at the end. This should help you out.

Or if you don't want to do that you obviously can just delete your post / comment where the user id matches what you want and then delete said users. This should help you as well if you prefer this solution.

3
  • can I use any of your solutions but set it from Java code, through JPA, and not modifying the DB manually?
    – catch23
    Jan 29, 2018 at 14:06
  • @nazar_art If you don't want to modify your current schema, you have to use the second option IMO. Create a query with JPA that you'll call from Java code and it should work fine.
    – Tapaka
    Jan 29, 2018 at 14:29
  • 1
    @nazar_art No, you can't have it both to automatically cascade, and to not have the dependent entities in BlogUser, and not modify the schema manually. You have to go with either one of the two solutions, or run the delete queries manually. Just make sure you don't have CascadeType.ALL on the @ManyToOne association, or it will recursively clear all three of those tables.
    – coladict
    Jan 29, 2018 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.