多对多映射的外键约束违规 (Postgresql)

问题描述 投票:0回答:1

我有一个双向多对多映射,我正在尝试删除一个帖子实体。当我删除它时, posts_categories 表中的引用也应该被删除。但是,尽管尝试了以下两种方法,但它并没有删除引用,并且我收到了以下错误。我正在使用 postgresql,但没有找到问题的解决方案。

错误:“posts”表上的 UPDATE 或 DELETE 操作违反了“posts_categories”表上的“fkq4402eed597exuj50g73c7kmg”外键约束。

第一种方法

@Entity
@Data
@Table(name = "posts")
public class PostEntity extends BaseEntity {

@Column(nullable = false)
private String title;


@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id", nullable = false)
private UserEntity author;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private PostStatus status;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
        name = "posts_categories",
        joinColumns = {@JoinColumn(name = "post_id")},
        inverseJoinColumns = {@JoinColumn(name = "category_id")})
private Set<CategoryEntity> categories = new HashSet<>();

@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
private List<CommentEntity> comments = new ArrayList<>();


@ElementCollection
@CollectionTable(name = "post_tags", joinColumns = @JoinColumn(name = "post_id"))
@Column(name = "tag")
private List<String> tags;

@Column(nullable = false)
private int views = 0;

@Column(nullable = false)
private int likes = 0;

@PrePersist
protected void onCreate() {
    super.onCreate();
    status = PostStatus.DRAFT;
}
} 


@Entity
@Data
@Table(name = "categories")
public class CategoryEntity extends BaseEntity {

@Column(nullable = false, unique = true)
private String name;

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "categories")
private Set<PostEntity> posts = new HashSet<>();    
}

第二种方法

@Entity
@Data
@Table(name = "posts")
public class PostEntity extends BaseEntity {

@Column(nullable = false)
private String title;


@Column(nullable = false, columnDefinition = "TEXT")
private String content;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "author_id", nullable = false)
private UserEntity author;

@Enumerated(EnumType.STRING)
@Column(nullable = false)
private PostStatus status;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
        name = "posts_categories",
        joinColumns = {@JoinColumn(name = "post_id")},
        inverseJoinColumns = {@JoinColumn(name = "category_id")})
private Set<CategoryEntity> categories = new HashSet<>();

@OneToMany(mappedBy = "post", cascade = CascadeType.REMOVE, orphanRemoval = true, fetch = FetchType.LAZY)
private List<CommentEntity> comments = new ArrayList<>();


@ElementCollection // basit türdeki veriler için ek bir tablo oluşturur
@CollectionTable(name = "post_tags", joinColumns = @JoinColumn(name = "post_id"))
@Column(name = "tag")
private List<String> tags;

@Column(nullable = false)
private int views = 0;

@Column(nullable = false)
private int likes = 0;

@PrePersist
protected void onCreate() {
    super.onCreate();
    status = PostStatus.DRAFT;
}
}


@Entity
@Data
@Table(name = "categories")
public class CategoryEntity extends BaseEntity {

@Column(nullable = false, unique = true)
private String name;

@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
        name = "posts_categories",
        joinColumns = {@JoinColumn(name = "category_id")},
        inverseJoinColumns = {@JoinColumn(name = "post_id")})
private Set<PostEntity> posts = new HashSet<>();
}

我的服务:

@Transactional
public Boolean deleteByUUID(UUID uuid) {
    Entity entity = postRepository.findByUuid(uuid).orElse(null);
    if (entity != null) {
        postRepository.delete(entity);
        return Boolean.TRUE;
    } else {
        return Boolean.FALSE;
    }
}
postgresql many-to-many bidirectional bidirectional-relation constraintviolationexception
1个回答
0
投票

你可以尝试与两个表建立外关系,当你从存在多对多关系的表中删除关系时,你可以很容易地看到两个表中匹配的行都被删除了

© www.soinside.com 2019 - 2024. All rights reserved.