Java 无法使用hibernate将删除级联到子实体

Java 无法使用hibernate将删除级联到子实体,java,spring,hibernate,jpa,cascade,Java,Spring,Hibernate,Jpa,Cascade,我有一个使用SpringBoot和hibernate的优惠券系统项目。 当试图删除一家公司时,我希望它删除所选的公司优惠券以及客户购买的所有已删除优惠券 使用cascade=CascadeType.ALL时 它可以毫无问题地删除公司及其优惠券,但是,优惠券ID的购买表仍然存在,并且尚未删除 我试过使用删除=true。但它什么也没用。 如果我使用CascadeType.REMOVE它只会删除购买历史记录中的所有内容,包括来自不同公司的优惠券购买历史记录 是否有使用Hibernate/Spring注

我有一个使用SpringBoot和hibernate的优惠券系统项目。 当试图删除一家公司时,我希望它删除所选的公司优惠券以及客户购买的所有已删除优惠券

使用
cascade=CascadeType.ALL时
它可以毫无问题地删除公司及其优惠券,但是,优惠券ID的购买表仍然存在,并且尚未删除

我试过使用
删除=true
。但它什么也没用。 如果我使用
CascadeType.REMOVE
它只会删除购买历史记录中的所有内容,包括来自不同公司的优惠券购买历史记录

是否有使用Hibernate/Spring注释删除公司所有历史记录的方法?或者我应该用本地的方式

我有
公司
表格,里面有所有公司 我有
优惠券
表,其中包含所有优惠券,并且有优惠券id的PK 我有
coups\u CUSTOMERS
表,该表保存客户的购买历史记录,两列都是PK键(customer\u id-coups\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 = "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);
    }
public interface CompanyRepository extends JpaRepository<Company, Integer> {

    Company findCompanyByEmailAndPassword(String email, String password);

    Company findCompanyByEmail(String email);

    Company findCompanyByName(String name);


}
优惠券类

    @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 = "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 = "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);
    }
public interface CompanyRepository extends JpaRepository<Company, Integer> {

    Company findCompanyByEmailAndPassword(String email, String password);

    Company findCompanyByEmail(String email);

    Company findCompanyByName(String name);


}
public interface CompanyRepository扩展了JpaRepository{
公司通过电子邮件和密码查找公司(字符串电子邮件、字符串密码);
公司通过电子邮件(字符串电子邮件)查找公司;
公司findCompanyByName(字符串名称);
}

你能分享你的
deleteById
方法吗?@ansvir您好,deleteById方法来自扩展我的存储库接口的JpaRepository。所以它不是我写的。不管怎样,我已经分享了这个报告,你使用的是最新的Hibernate版本5.4.30吗?如果是这样,并且您仍然存在问题,请在issue tracker()中创建一个问题,并使用一个复制该问题的测试用例()。