java - 将 @OneToMany 实体与子实体一起保存(带有复合外键的 @EmbeddedId)

标签 java hibernate jpa

我有这些表:

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)
);
  • 一家公司可以拥有多名员工
  • 公司的主键是由 3 个字段组成的复合键
  • 员工的主键由公司的复合键(即外键)以及公司特定的另一个字段组成。

基于此相关问题:

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

我创建了以下实体:

@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
}

我想拯救一家只有一名员工的公司,并让它写一份 行到公司表和员工表,但每当我运行 接下来,我只看到一行被写入 Company 表 而从来没有员工表?

我不确定下面的方法是否正确,或者可能是实体 以上是否正确?

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?

非常感谢!

最佳答案

您正在使用公司存储库保存公司,但该公司没有级联注释。

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

应该是:

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

关于java - 将 @OneToMany 实体与子实体一起保存(带有复合外键的 @EmbeddedId),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60092735/

相关文章:

java - JPA 如何识别两个具有相同名称但位于不同包中的类?

java - 我需要一个正则表达式模式,它匹配数字后跟点最多 5 次,不带空格

java - 使 session spring 安全无效

java - 无效数据访问ApiUsageException : detached entity passed to persist

java - 在 Hibernate 中建模限定关系

java - 来自单个 validator 的集合验证

java - libGDX : Scrollpane is child of Scrollpane and switch between them in scrolling

java - 当输入为 "???"时,StringUtils isNumeric 返回 true,为什么?

java - 在 hibernate 中一对多单向添加更多对象到现有列表中

java - 两个嵌套的 EJB Bean - 只有第一个被注入(inject)了 entitymanager