You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring JPA deleteById无法删除实体问题求助

Spring JPA deleteById无法删除实体问题求助

大家好,我最近卡在一个头疼的问题上——尝试用Spring JPA的deleteById方法删除CharacterEntity实体对象,但不管怎么试都没效果,完全删不掉。

下面是相关的实体类代码,麻烦各位帮忙看看哪里可能出问题了:

CharacterEntity 代码

@Entity
@Table(name = "CHARACTERS")
class CharacterEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = 0,
    @ManyToOne
    @JoinColumn(name = "BANK_ID", nullable = false)
    val bank: BankEntity,
    @Column(nullable = true)
    val playerId: Long?,
    @Column(nullable = false)
    val name: String,
    @OneToMany(fetch = EAGER, cascade = [ALL], mappedBy = "character", orphanRemoval = true)
    val accounts: MutableList<AccountEntity> = mutableListOf()
) {
    @OneToOne(fetch = EAGER, cascade = [ALL], mappedBy = "character", orphanRemoval = true)
    val downtimeOverride: DowntimeOverride = DowntimeOverride(0, this)
}

AccountEntity 部分代码

@Entity
@Table(name = "ACCOUNTS")
class AccountEntity(
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    val id: Long = 0,
    @ManyToOne
    @JoinColumn(/* 此处代码截断,应为关联CharacterEntity的配置 */)
    // 剩余代码未完整提供
)

我已经在关联关系上配置了cascade = [ALL]和orphanRemoval = true,按道理应该会级联删除关联的子实体,但现在连主实体都删不掉。有没有大佬能指点下可能的原因?比如是不是事务配置的问题,或者关联关系的映射哪里写错了?

备注:内容来源于stack exchange,提问作者The Mather1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.17 07:34:28