【问题标题】:Split huge sql table logically into smaller tables将庞大的 sql 表逻辑拆分为较小的表
【发布时间】:2021-12-16 09:26:30
【问题描述】:

我正在使用 Hibernate ORM 框架制作一个 Spring Boot 应用程序。

我在那里有Employee 实体:

@Entity
public class Employee {
    private String firstName;
    private String position;

    //// more than 30 private fields

    //// fields related to one sublogic
    private String category;
    private LocalDate categoryAssignmentDate;
    private LocalDate categoryAssignmentDeadlineDate;
    private LocalDate docsSubmitDeadlineDate;
}

Employee 类中有 30 多个私有字段。

如您所见,我有 4 个字段与同一子逻辑 Category 相关。

所以我的问题是:将我的Employee 实体拆分为两个实体EmployeeCategory,这将作为OnetoOne 关系连接是一个好习惯吗? p>

是否让代码更清晰?

【问题讨论】:

  • 可能,是的。这将由database normalization 确定。给定的属性列应仅取决于表的主键。如果该属性依赖于另一个属性,则它属于另一个表。

标签: sql jpa database-design database-normalization


【解决方案1】:

使用embedded and embeddable 防止双表映射和不必要的OneToOne 关系。

@Entity
public class Employee {
    private String firstName;
    private String position;

    @Embedded
    private Category category
}

@Embeddable
public class Category{
    private String category;
    private LocalDate categoryAssignmentDate;
    private LocalDate categoryAssignmentDeadlineDate;
    private LocalDate docsSubmitDeadlineDate;
}

您可能需要添加attribute overrides

【讨论】:

    猜你喜欢
    • 2012-08-18
    • 2012-07-12
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    相关资源
    最近更新 更多