无法删除或更新父行:外键约束失败 Spring JPA

发布于 2025-01-11 02:48:51 字数 1659 浏览 0 评论 0原文

我有这个查询

     DELETE
     FROM bookings as b
     WHERE b.check_out = CURRENT_DATE;

我得到

无法删除或更新父行:外键约束失败 (online_booking_app.booked_rooms, CONSTRAINT FK3x1lpikb2vk75nx41lxhdicvn FOREIGN KEY (booking_id)参考预订 (id))

我的 Booking 实体有 CascadeType.ALL 并由另一侧的匹配映射 - 根据我的研究,这些是可能导致此消息的一些错误。

这是预订实体:

@Entity
@Table(name = "bookings")
public class BookingEntity extends BaseEntity {


    @OneToMany(mappedBy = "booking",cascade = CascadeType.ALL, orphanRemoval = true)
    private List<BookedRoomsEntity> bookedRooms = new ArrayList<>();

    private String firstName;
    private String lastName;
    
        public List<BookedRoomsEntity> getBookedRooms() {
        return bookedRooms;
    }

    public BookingEntity setBookedRooms(List<BookedRoomsEntity> bookedRooms) {
        this.bookedRooms = bookedRooms;
        return this;
    }

预订房间实体

@Entity
@Table(name = "booked_rooms")
public class BookedRoomsEntity extends BaseEntity {

    @ManyToOne()
    private BookingEntity booking;
    
    
        public BookingEntity getBooking() {
        return booking;
    }

    public BookedRoomsEntity setBooking(BookingEntity booking) {
        this.booking = booking;
        return this;
    }

I have this query

     DELETE
     FROM bookings as b
     WHERE b.check_out = CURRENT_DATE;

and I get

Cannot delete or update a parent row: a foreign key constraint fails (online_booking_app.booked_rooms, CONSTRAINT FK3x1lpikb2vk75nx41lxhdicvn FOREIGN KEY (booking_id) REFERENCES bookings (id))

My Booking entity has CascadeType.ALL and mapped by matches the other side - from my research these are some of the mistakes that could lead to this message.

Here is the BookingEntity:

@Entity
@Table(name = "bookings")
public class BookingEntity extends BaseEntity {


    @OneToMany(mappedBy = "booking",cascade = CascadeType.ALL, orphanRemoval = true)
    private List<BookedRoomsEntity> bookedRooms = new ArrayList<>();

    private String firstName;
    private String lastName;
    
        public List<BookedRoomsEntity> getBookedRooms() {
        return bookedRooms;
    }

    public BookingEntity setBookedRooms(List<BookedRoomsEntity> bookedRooms) {
        this.bookedRooms = bookedRooms;
        return this;
    }

BookedRoomsEntity

@Entity
@Table(name = "booked_rooms")
public class BookedRoomsEntity extends BaseEntity {

    @ManyToOne()
    private BookingEntity booking;
    
    
        public BookingEntity getBooking() {
        return booking;
    }

    public BookedRoomsEntity setBooking(BookingEntity booking) {
        this.booking = booking;
        return this;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2025-01-18 02:48:51

CascadeType 仅适用于 EntityManager 操作。

因此,您有两个选择:

  • 首先加载要删除的实体,然后使用 EntityManager.remove
  • 首先使用单独的 JPQL 语句删除引用实体。

The CascadeType does only apply to EntityManager operations.

You therefore have two options:

  • Load the entities to be deleted first and then use EntityManager.remove
  • Remove the referencing entities first with a separate JPQL statement.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文