当父母"以及如何在JPA中建立一对一的关系模型。表有复合PK吗?

时间:2016-08-01 10:10:44

标签: java hibernate jpa one-to-one natural-key

虽然有很多关于如何建模的信息,但在JPA(2)中,一对一的关系 一个拥有自然密钥的实体,我还没有找到一个明确/简单的答案来解决我们两者都有的情况,即 一个 - 父表具有自然键的一对一关系 。很明显,我可能错过了这样一个教程;如果是这样的话,指点一个也可能就是答案。

而且,就像JPA和我这样的新手一样多次,当一个人需要比最基本的模型更多的时候,人们很快就能碰壁。

因此,考虑以下数据库模型:

enter image description here

相应的JPA注释对象模型是什么? (因为我不想影响答案,所以我已经把你们所做过的事情饶恕了。)

也欢迎性能建议(例如"一对多可以更快地执行"等等)!

谢谢,

1 个答案:

答案 0 :(得分:13)

映射复合键并不困难,如following article

中所示

复合标识符由两个数字列构成,因此映射如下所示:

@Embeddable
public class EmployeeId implements Serializable {

    private Long companyId;

    private Long employeeId;

    public EmployeeId() {
    }

    public EmployeeId(Long companyId, Long employeeId) {
        this.companyId = companyId;
        this.employeeId = employeeId;
    }

    public Long getCompanyId() {
        return companyId;
    }

    public Long getEmployeeId() {
        return employeeId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof EmployeeId)) return false;
        EmployeeId that = (EmployeeId) o;
        return Objects.equals(getCompanyId(), that.getCompanyId()) &&
                Objects.equals(getEmployeeId(), that.getEmployeeId());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getCompanyId(), getEmployeeId());
    }
}

父类,如下所示:

@Entity(name = "Employee")
public static class Employee {

    @EmbeddedId
    private EmployeeId id;

    private String name;

    @OneToOne(mappedBy = "employee")
    private EmployeeDetails details;

    public EmployeeId getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public EmployeeDetails getDetails() {
        return details;
    }

    public void setDetails(EmployeeDetails details) {
        this.details = details;
    }
}

孩子喜欢这样:

@Entity(name = "EmployeeDetails")
public static class EmployeeDetails {

    @EmbeddedId
    private EmployeeId id;

    @MapsId
    @OneToOne
    private Employee employee;

    private String details;

    public EmployeeId getId() {
        return id;
    }

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

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
        this.id = employee.getId();
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }
}

一切正常:

doInJPA(entityManager -> {
    Employee employee = new Employee();
    employee.setId(new EmployeeId(1L, 100L));
    employee.setName("Vlad Mihalcea");
    entityManager.persist(employee);
});

doInJPA(entityManager -> {
    Employee employee = entityManager.find(Employee.class, new EmployeeId(1L, 100L));
    EmployeeDetails employeeDetails = new EmployeeDetails();
    employeeDetails.setEmployee(employee);
    employeeDetails.setDetails("High-Performance Java Persistence");
    entityManager.persist(employeeDetails);
});

doInJPA(entityManager -> {
    EmployeeDetails employeeDetails = entityManager.find(EmployeeDetails.class, new EmployeeId(1L, 100L));
    assertNotNull(employeeDetails);
});
doInJPA(entityManager -> {
    Phone phone = entityManager.find(Phone.class, "012-345-6789");
    assertNotNull(phone);
    assertEquals(new EmployeeId(1L, 100L), phone.getEmployee().getId());
});

GitHub上提供的代码。