我有以下实体。
这是一个优惠券购买系统。它为每个实体创建3个表,并以customer_coupons的名称创建另一个表,该表通过外键保存所有客户的采购信息
在customer_coupons数据库中跟踪DDL
CREATE TABLE `customer_coupons` (
`customer_id` int NOT NULL,
`coupons_id` int NOT NULL,
KEY `FK3ra7y4e2fu00kui0lby4mj0w1` (`coupons_id`),
KEY `FK2xh7flxxfqpn6prhw5n06l4nn` (`customer_id`),
CONSTRAINT `FK2xh7flxxfqpn6prhw5n06l4nn` FOREIGN KEY (`customer_id`) REFERENCES `customer` (`id`) ON DELETE CASCADE,
CONSTRAINT `FK3ra7y4e2fu00kui0lby4mj0w1` FOREIGN KEY (`coupons_id`) REFERENCES `coupon` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci公司级:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(nullable = false, unique = true)
private String name;
@Column(nullable = false, unique = true)
private String email;
private String password;
@Singular
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER, mappedBy = "company")
@OnDelete(action = OnDeleteAction.CASCADE)
private List<Coupon> coupons;客户类别:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String firstName;
private String lastName;
@Column(nullable = false, unique = true)
private String email;
private String password;
@Singular
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OnDelete(action = OnDeleteAction.CASCADE)
private List<Coupon> coupons;优惠券类别:
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Data
public class Coupon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne()
@ToString.Exclude
private Company company;
@Enumerated(EnumType.STRING)
private Category category;
private String title;
private String description;
private Date startDate;
private Date endDate;
private int amount;
private double price;
private String image;我要创建一个级联删除,当您删除DB中的一个公司时,它将删除所有它的优惠券,它将删除所有的优惠券购买。(这是一样的,当您删除一个客户,它不删除优惠券,但删除在联接表中的购买)
因此,我的问题是,如何在表的外键coupons_id中添加on级联选项
发布于 2021-08-26 08:39:49
您可以用@JoinTable注释为两边定义@ForeignKey注释的外键。在该定义中,您可以指定ON DELETE CASCADE选项。
https://stackoverflow.com/questions/68740747
复制相似问题