rmf
rmf

Reputation: 65

Saving @OneToMany entity along with children (@EmbeddedId with composite foreign key)

I have these tables:

CREATE TABLE company (
    id VARCHAR(256) NOT NULL,
    tenantId VARCHAR(256) NOT NULL,
    fieldName VARCHAR(256) NOT NULL,
    PRIMARY KEY (id, tenantId, fieldName)
);

CREATE TABLE employee (
    tenantId VARCHAR(256) NOT NULL,
    companyFieldName VARCHAR(256) NOT NULL,
    companyId VARCHAR(256) NOT NULL,
    fieldName VARCHAR(256) NOT NULL,
    PRIMARY KEY (tenantId, companyFieldName, companyId, fieldName),
    CONSTRAINT fkCompany FOREIGN KEY (tenantId, companyFieldName, companyId) REFERENCES employee (tenantId, fieldName, id)
);

Based on this related question:

JPA how to make composite Foreign Key part of composite Primary Key

I have created the following entities:

@Embeddable
public class CompanyIdentity implements Serializable
{
    @NotBlank
    private String tenantId;

    @NotBlank
    private String fieldName;

    @NotBlank
    private String id;

    //getters, setters, equals and hashcode ommited
}

@Entity
public class Company implements Serializable
{
    @EmbeddedId
    private CompanyIdentity companyIdentity;

    @OneToMany(mappedBy = "company")
    private Set<Employee> employees;

    //getters, setters, equals and hashcode ommited
}

@Embeddable
public class EmployeeIdentity implements Serializable
{
    @NotNull
    private CompanyIdentity companyIdentity;

    // This *not* the fieldName in the CompanyIdentity, this is a property
    // specific to an employee, it just happens to have the same name
    @NotBlank
    private String fieldName; 

    //getters, setters, equals and hashcode ommited
}

public class Employee implements Serializable
{
    @EmbeddedId
    private EmployeeIdentity employeeIdentity;

    @MapsId("companyIdentity")
    @JoinColumns({
        @JoinColumn(name = "tenantId", referencedColumnName = "tenantId"),
        @JoinColumn(name = "companyFieldName", referencedColumnName = "fieldName"),
        @JoinColumn(name = "companyId", referencedColumnName = "id")
    })
    @ManyToOne
    @Cascade(value={org.hibernate.annotations.CascadeType.ALL})
    private Company company;

    //getters, setters, equals and hashcode ommited
}

I want to save a company with a single employee, and have it write a row to the Company table and Employee table, but whenever I run the following, I only ever see a row getting written to the Company table and never the Employee table?

I'm not sure if below is the right approach or not, or maybe the entities above are not correct?

public interface CompanyRepository extends CrudRepository<Company, String> {}
final Company company = new Company();
final CompanyIdentity companyIdentity = new CompanyIdentity("company-tenant-id", "company-field-name", "company-id");

company.setCompanyIdentity(companyIdentity);

final Employee employee = new Employee();

final EmployeeIdentity employeeIdentity = new EmployeeIdentity();
employeeIdentity.setFieldName("employee-field-name");
employeeIdentity.setCompanyIdentity(companyIdentity);

employee.setEmployeeIdentity(employeeIdentity);

employee.setCompany(company);

final Set<Employee> employees = new HashSet<>();
employees.add(employee);

company.setEmployees(employees);

companyRepository.save(company); //only saves company, not employee?

Many thanks!

Upvotes: 1

Views: 1490

Answers (1)

Klaas
Klaas

Reputation: 80

Your are saving the company with the company repository but the company doens´t have a cascade annotation.

@OneToMany(mappedBy = "company")
private Set<Employee> employees;

Should be:

@OneToMany(mappedBy = "company")
@Cascade(value={org.hibernate.annotations.CascadeType.ALL})
private Set<Employee> employees;

Upvotes: 1

Related Questions