Jpa 如果外键约束失败,则无法删除或更新父行
无法删除或更新父行:外键约束失败(Jpa 如果外键约束失败,则无法删除或更新父行,jpa,spring-data-jpa,Jpa,Spring Data Jpa,无法删除或更新父行:外键约束失败(gopickorders\u paid\u items,约束fkeeeefhbl6j5xhs7nnt5mn530f8外键(paid\u product\u id,paid\u items\u user\u id)引用cart(product id,用户id>) 购物车实体如下 @Entity @ToString @EqualsAndHashCode @IdClass(CartIdPk.class) public class
gopick
orders\u paid\u items
,约束fkeeeefhbl6j5xhs7nnt5mn530f8
外键(paid\u product\u id
,paid\u items\u user\u id
)引用cart
(product id
,用户id>)
购物车实体如下
@Entity
@ToString
@EqualsAndHashCode
@IdClass(CartIdPk.class)
public class Cart implements Serializable {
@Column(unique = true)
private Long id = Long.parseLong(String.format("%06d", new Random().nextInt(999999)));
@JsonIgnore
@Id
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
private CartStatus cartStatus = CartStatus.IN_CART;
@Id
private int productId;
private int quantity = 0;
@Column(length = 10, nullable = true)
private String discount;
@Column(length = 30, nullable = true)
private String paymentRef;
@JsonIgnore
@Column(insertable = false, updatable = true)
@UpdateTimestamp
private Timestamp lastModified;
@CreationTimestamp
private Timestamp dateCreated;
}
@lombok.Data
@Entity
public class Orders implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@OneToMany(fetch = FetchType.LAZY)
private Collection<Cart> paidItems;
@JsonIgnore
@Column(insertable = false, updatable = true)
@UpdateTimestamp
private Timestamp lastModified;
@CreationTimestamp
private Timestamp dateCreated;
}
Id类对象CartIdPk
@NoArgsConstructor
@AllArgsConstructor
@lombok.Data
public class CartIdPk implements Serializable {
private Long user;
private int productId;
}
订单实体如下
@Entity
@ToString
@EqualsAndHashCode
@IdClass(CartIdPk.class)
public class Cart implements Serializable {
@Column(unique = true)
private Long id = Long.parseLong(String.format("%06d", new Random().nextInt(999999)));
@JsonIgnore
@Id
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
private CartStatus cartStatus = CartStatus.IN_CART;
@Id
private int productId;
private int quantity = 0;
@Column(length = 10, nullable = true)
private String discount;
@Column(length = 30, nullable = true)
private String paymentRef;
@JsonIgnore
@Column(insertable = false, updatable = true)
@UpdateTimestamp
private Timestamp lastModified;
@CreationTimestamp
private Timestamp dateCreated;
}
@lombok.Data
@Entity
public class Orders implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@OneToMany(fetch = FetchType.LAZY)
private Collection<Cart> paidItems;
@JsonIgnore
@Column(insertable = false, updatable = true)
@UpdateTimestamp
private Timestamp lastModified;
@CreationTimestamp
private Timestamp dateCreated;
}
在java中使用JPA和ORMs的经验法则是记住,您同时拥有一个对象模型和一个关系数据库模型,并且必须在这两个模型中进行更改
因此,订单和购物车之间存在1对多的差异
因此,为了便于讨论,您尝试删除当前在订单中的购物车。当保存发生时,您将得到一个完整性冲突,因为您试图删除的购物车仍然被订单引用
您需要做的是在删除之前从卡中删除订单,因为您在订单中引用了购物车
始终尝试在“对象”级别和数据库级别进行更改。因此,我所做的只是从productId中删除id注释,并删除嵌入的id类,因此在插入之前,我会根据我的用例进行手动集成检查,效果良好