I have this project of a coupon system using spring boot and hibernate. While trying to delete a company I want it to delete selectd company coupons and all purchased made by customer of those deleted coupons.
when using cascade = CascadeType.ALL
it removes the company and its coupons with no problem, however, the purchase table with coupon ID still exists and has not been deleted.
I tried using orphanRemoval=true. But it doesn't do anything.
If I use CascadeType.REMOVE it just removes everything in purchase history including coupons purchase history from different companies.
Is there anyway of deleting all history of company using Hibernate/Spring annotations? or I should just use the native way.
I have COMPANY table which holds all companies
I have COUPON table which holds all coupon and has a PK of coupon_id
I have COUPONS_CUSTOMERS table which holds the purchase history of customer and both columns are PK keys (customer_id - coupon_id). (this is the touble maker table).
Company Class:
@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;
}
Remove Method:
public void deleteCompany(int id) throws CompanyDoesNotExistException {
if (compRepo.existsById(id)) {
compRepo.deleteById(id);
} else {
throw new CompanyDoesNotExistException();
}
}
Coupon Class
@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;
}
Customer Class:
@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);
}
Deleting Company
admin.deleteCompany(1);
I do not get any exception, however, I cannot seem to find the solution to delete the whole history of the company even tho it removes companies coupons it still keeps the purchase of those coupons.
Hope I am clear. I could add additional information if needed.
Repository:
public interface CompanyRepository extends JpaRepository<Company, Integer> {
Company findCompanyByEmailAndPassword(String email, String password);
Company findCompanyByEmail(String email);
Company findCompanyByName(String name);
}