我有一个使用springboot和hibernate的优惠券系统的项目。当试图删除一个公司,我希望它删除选定的公司优惠券和所有购买的客户所做的那些删除优惠券。
使用时 cascade = CascadeType.ALL
它删除公司及其优惠券没有问题,但是,优惠券id的采购表仍然存在,并且没有被删除。
我试过用 orphanRemoval=true
. 但它什么也做不了。如果我使用 CascadeType.REMOVE
它只是删除了购买历史记录中的所有内容,包括来自不同公司的优惠券购买历史记录。
是否有使用hibernate/spring注解删除公司所有历史记录的方法?或者我应该用本地的方式。
我有 COMPANY
我所有公司的table COUPON
包含所有优惠券并有优惠券id pk的表 COUPONS_CUSTOMERS
保存客户的购买历史记录的表,两列都是pk键(customer\u id-coupon\u id)(这是touble maker表)。
公司等级:
@Entity
@Table(name = "companies")
@Data
//without this we get a circular dependency, while excluding coupons from to string we fix this problem
@ToString(exclude = "coupons")
@NoArgsConstructor
public class Company {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.PRIVATE)
private int companyId;
@Column(nullable = false, unique = true)
@Setter(AccessLevel.PRIVATE)
private String name;
@Email(message = "Wrong email input. Please try again")
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
@Size(min = 4, max = 8, message = "Password range must be between 4 - 8 digits")
private String password;
@OneToMany(mappedBy = "company", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Coupon> coupons;
public Company(String name, String email, String password) {
this.name = name;
this.email = email;
this.password = password;
}
public Company(int companyId, String name, String email, String password) {
this.companyId = companyId;
this.name = name;
this.email = email;
this.password = password;
}
删除方法:
public void deleteCompany(int id) throws CompanyDoesNotExistException {
if (compRepo.existsById(id)) {
compRepo.deleteById(id);
} else {
throw new CompanyDoesNotExistException();
}
}
优惠券类别
@Entity
@Table(name = "coupons")
@Data()
@NoArgsConstructor
public class Coupon {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.PRIVATE)
private int couponId;
@ManyToOne(mappedby = "")
private Company company;
@Column(nullable = false)
private Category category;
@Column(nullable = false)
private String title;
@Column(nullable = false)
private String description;
@Column(name = "start_date", nullable = false)
@FutureOrPresent(message = "\nInvalid date.\nDate must be present or future")
private Date startDate;
@Column(name = "end_date", nullable = false)
@Future(message = "\nInvalid date.\nEnd date must be a future date")
private Date endDate;
@Column(nullable = false)
@PositiveOrZero( message = "Cannot add a negative amount")
private int amount;
@Column(nullable = false)
@Min(value = 10, message = "Price cannot be under 10$")
private double price;
@Column(nullable = false)
private String image;
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "coupons", cascade = CascadeType.ALL)
private Set<Customer> customers;
public Coupon(Company company, Category category, String title, String description, Date startDate, Date endDate,
@PositiveOrZero int amount, @Min(10) double price, String image) {
this.company = company;
this.category = category;
this.title = title;
this.description = description;
this.startDate = startDate;
this.endDate = endDate;
this.amount = amount;
this.price = price;
this.image = image;
}
客户类别:
@Entity
@Table(name = "customers")
@NoArgsConstructor
@Getter
@Setter
@ToString
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Setter(AccessLevel.PRIVATE)
@Column(name = "customer_id")
private int customerId;
@Column(name = "first_name", nullable = false)
private String firstName;
@Column(name = "last_name", nullable = false)
private String lastName;
@Email(message = "Wrong email input, please try again")
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
@Size(min = 4, max = 8, message = "Password range must be between 4 -8 digits")
private String password;
@ManyToMany(fetch = FetchType.EAGER)
private Set<Coupon> coupons;
public Customer(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}
public void deleteCoupon(Coupon coupon) {
this.coupons.remove(coupon);
}
删除公司
admin.deleteCompany(1);
我没有得到任何例外,然而,我似乎找不到解决办法删除整个公司的历史,即使它删除了公司优惠券,但仍然保留购买这些优惠券。
希望我明白。如果需要,我可以添加其他信息。
存储库:
public interface CompanyRepository extends JpaRepository<Company, Integer> {
Company findCompanyByEmailAndPassword(String email, String password);
Company findCompanyByEmail(String email);
Company findCompanyByName(String name);
}
暂无答案!
目前还没有任何答案,快来回答吧!