我试图删除家长学生或家长课程,但出现以下错误:

Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "student" violates foreign key constraint "fkeyvuofq5vwdylcf78jar3mxol" on table "registration"

RegistrationId类是注册类中使用的复合密钥.我使用的是Spring数据jpa和Spring boot.

我做错了什么?我知道那种类型.当父对象被删除时,all也应该删除子对象,但它会给我一个错误.

@Embeddable
public class RegistrationId implements Serializable {

  @JsonIgnoreProperties("notifications")
  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name = "student_pcn", referencedColumnName="pcn")
  private Student student;

  @JsonIgnoreProperties({"teachers", "states", "reviews"})
  @OneToOne(cascade=CascadeType.ALL)
  @JoinColumn(name = "course_code", referencedColumnName="code")
  private Course course;


注册班

@Entity(name = "Registration")
@Table(name = "registration")
public class Registration {

@EmbeddedId
private RegistrationId id;

推荐答案

我通过使用hibernate@OnDelete注释实现了这一点.一些人是如何看待JPA的.持久性类型不起作用.它们对我 Select 的任何一个都没有影响.

就像下面一样.现在我可以删除家长学生或家长课程,所有子元素(注册)都会随之删除.

@Embeddable
public class RegistrationId implements Serializable {

    @JsonIgnoreProperties("notifications")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @OneToOne
    @JoinColumn(name = "student_pcn", referencedColumnName="pcn")
    private Student student;

    @JsonIgnoreProperties({"teachers", "states", "reviews"})
    @OnDelete(action = OnDeleteAction.CASCADE)
    @OneToOne
    @JoinColumn(name = "course_code", referencedColumnName="code")
    private Course course;

Postgresql相关问答推荐

将real[]数组作为完整的浮点值从postquist返回

使用PythonPolar在PostgreSQL中存储

为MCV扩展统计设置统计目标

Psql:Windows 10上奇怪的错误输出编码

我可以将jsonb_set与来自SELECT语句的新值(第三个参数)一起使用吗?

在rust中,有什么方法可以从sqlx::query_with中返回rows_infected以及行本身?

Postgres 14 反斜杠 Z 没有给出正确的输出

在 KnexJS 中插入

如何获得区间的绝对值

在 Postgres 中查询 JSON 对象数组

如何在 PSQL 中查找从另一个表继承的子表

postgresql查询中的正则表达式不区分大小写

PostgreSQL 返回准确或最接近查询日期的日期

SQLAlchemy、Psycopg2 和 Postgresql 复制

如何为查询执行设置语句超时

PostgreSQL 获取过go 12 小时的元素

从长时间的postgres转换日期

如何从 CSV 为 PostgreSQL 副本生成模式

PostgreSQL 中是否有类似 zip() 函数的东西,它结合了两个数组?

如何在 postgres 查询中排名