Jhipster returns org.hibernate.StaleStateException:批量更新从更新 [0] 返回了意外的行数;实际行数:0;预期:1
Jhipster returns org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
我在微服务架构(注册表、网关、uaa 服务器)中使用 JHipster。我使用配置文件实体扩展了我的 uaa 服务器内的默认 jhipster 用户(根据本文使用@mapsId 注释和一对一关系:https://www.jhipster.tech/tips/022_tip_registering_user_with_additional_information.html)。
我的问题如下:如果我在 jhipster 网关中注册一个新用户,我的配置文件将创建并写入数据库,用户和配置文件之间使用共享 ID,一切正常。现在,如果我想删除个人资料实体,用户实体也应该被删除(因为没有没有个人资料的用户)但我得到以下异常:
org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
如果我删除用户实体,我的个人资料也会被删除,因此级联应该可以工作。
用户实体:
@Entity
@Table(name = "jhi_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
...
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private Profile profile;
配置文件实体
@Entity(name = "Profile")
@Table(name = "profile")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Profile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
...
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "id")
@MapsId
private User user;
使用 liquibase 的配置文件 table 的外键约束:
<addForeignKeyConstraint baseColumnNames="id"
baseTableName="profile"
constraintName="fk_profile_user_id"
referencedColumnNames="id"
referencedTableName="jhi_user"
onDelete="CASCADE"
/>
我在这里错过了什么吗?我还尝试使用 hibernate 注释而不是 JPA 注释,但没有改变任何东西,所以我认为这可能是 Hibernate 本身的问题。
我通过在个人资料 table 中添加新的 "user_id" 列解决了我的问题。我向 id 列(用户 table)和我的 user_id 列(配置文件 table)添加了一个外键约束。不完全是我想要的,但它有效。只要确保在关系的两边都启用级联,那么当您删除它的用户时,配置文件会自动删除。
我在微服务架构(注册表、网关、uaa 服务器)中使用 JHipster。我使用配置文件实体扩展了我的 uaa 服务器内的默认 jhipster 用户(根据本文使用@mapsId 注释和一对一关系:https://www.jhipster.tech/tips/022_tip_registering_user_with_additional_information.html)。
我的问题如下:如果我在 jhipster 网关中注册一个新用户,我的配置文件将创建并写入数据库,用户和配置文件之间使用共享 ID,一切正常。现在,如果我想删除个人资料实体,用户实体也应该被删除(因为没有没有个人资料的用户)但我得到以下异常:
org.springframework.orm.ObjectOptimisticLockingFailureException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
如果我删除用户实体,我的个人资料也会被删除,因此级联应该可以工作。
用户实体:
@Entity
@Table(name = "jhi_user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
...
@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private Profile profile;
配置文件实体
@Entity(name = "Profile")
@Table(name = "profile")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Profile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private Long id;
...
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "id", referencedColumnName = "id")
@MapsId
private User user;
使用 liquibase 的配置文件 table 的外键约束:
<addForeignKeyConstraint baseColumnNames="id"
baseTableName="profile"
constraintName="fk_profile_user_id"
referencedColumnNames="id"
referencedTableName="jhi_user"
onDelete="CASCADE"
/>
我在这里错过了什么吗?我还尝试使用 hibernate 注释而不是 JPA 注释,但没有改变任何东西,所以我认为这可能是 Hibernate 本身的问题。
我通过在个人资料 table 中添加新的 "user_id" 列解决了我的问题。我向 id 列(用户 table)和我的 user_id 列(配置文件 table)添加了一个外键约束。不完全是我想要的,但它有效。只要确保在关系的两边都启用级联,那么当您删除它的用户时,配置文件会自动删除。