M
M
Maks Burkov2017-04-05 11:06:48
Hibernate
Maks Burkov, 2017-04-05 11:06:48

How to configure Hibernate to create a specific type of table?

I use hibernate to create tables.
From the error, I understand the table is created in such a way that it is not possible to delete or update the parent ..
How to configure hibernate to create a table with the ability to update or delete the parent?

Cannot delete or update a parent row: a foreign key constraint fails (`couponsystem`.`company_employees`, CONSTRAINT `FK8qf6rpgr5asjrokumh155o3jr` FOREIGN KEY (`COMPANY_ID`) REFERENCES `companies` (`COMPANY_ID`))

DELETE FROM companies WHERE COMPANY_ID = "8768765";

@Entity
@Table(name = "companies")
public class Company implements Serializable{

    public Company() {}

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Company company = (Company) o;

        if (id != company.id) return false;
        if (!compName.equals(company.compName)) return false;
        if (!password.equals(company.password)) return false;
        return email.equals(company.email);
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + compName.hashCode();
        result = 31 * result + password.hashCode();
        result = 31 * result + email.hashCode();
        return result;
    }

    public Collection<Customer> getCompany_customers() {
        return company_customers;
    }

    public void setCompany_customers(Collection<Customer> company_customers) {
        this.company_customers = company_customers;
    }

    public Collection<Coupon> getCompany_coupons() {
        return company_coupons;
    }

    public void setCompany_coupons(Collection<Coupon> company_coupons) {
        this.company_coupons = company_coupons;
    }

    @OneToMany
    @JoinTable(name = "company_customers" , joinColumns = @JoinColumn(name = "COMPANY_ID") , inverseJoinColumns = @JoinColumn(name = "CUSTOMER_ID"))
    private Collection<Customer> company_customers;

    @OneToMany
    @JoinTable(name = "company_coupons" , joinColumns = @JoinColumn(name = "COMPANY_ID") , inverseJoinColumns = {@JoinColumn(name = "COUPON_ID")})
    private Collection<Coupon> company_coupons;

    @OneToMany
    @JoinTable(name = "company_employees" , joinColumns = @JoinColumn(name = "COMPANY_ID") , inverseJoinColumns = @JoinColumn(name = "EMPLOYEE_ID"))
    private Collection<Employee> company_employee;

    @Id
    @Column(name = "COMPANY_ID")
    @Size(min = 5 , max = 20 , message = "Company id must be 5 - 20")
    private long id;
    @Column(name = "COMPANY_NAME")
    @NotBlank(message = "Company name can't be empty! ")
    private String compName;

    @Column(name = "COMPANY_PASSWORD")
    @Pattern(regexp = "((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%]).{6,20})",
            message = "Wrong password! Correct examples: " +
                    "1. [email protected]\n" +
                    "2. mkYOn12$")
    private String password;

    @Column(name = "COMPANY_EMAIL")
    @Pattern(regexp = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
            + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",
            message = "Wrong email! Correct examples: " +
                    "1. [email protected], [email protected], [email protected]\n" +
                    "2. [email protected], [email protected], [email protected]\n" +
                    "3. [email protected], [email protected]\n" +
                    "4. [email protected], [email protected]")
    private String email;

    @Override
    public String toString() {
        return "Company [id=" + id + ", compName=" + compName + ", password=" + password + ", email=" + email +"]";
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getCompName() {
        return compName;
    }

    public void setCompName(String compName) {
        this.compName = compName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-10
@Maks00088

After each @OneToMany annotation, add the @Cascade(CascadeType.DELETE) annotation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question