sharp-database中的BaseDAOImpl通过注解@Embedded实现实体的”组件”

@Embedded 作用是将一个POJO作为实体的组成部分,但还是生成一张表。与 @ManyToOne OneToMany 不同的是,它们会生成新的表。

示例如下:

Company.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Table(value = "t_company", comment = "公司")
public class Company extends SimpleEntity {

    private String name;

    private String address;

    private String phone;

    @Embedded
    private ContactPerson contactPerson;

}

ContactPerson.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ContactPerson {

    private String firstName;

    @Column(updatable = false)
    private String lastName;

    @Column("contact_phone")
    private String phone;

    @OneToMany(subTable = "t_company_vendor", joinValue = "company_id", cascadeInsertOrUpdate = true, reversePropertyName = "companyId")
    private List<Vendor> vendorList;
}

ContactPerson 只是 Company 的组成部分,本身不会生成新的表。
下面的示例 @Embedded,从数据库层面的设计,下面等价。

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
@Table(value = "t_company", comment = "公司")
public class Company extends SimpleEntity {

    private String name;

    private String address;

    private String phone;

    private String firstName;

    @Column(updatable = false)
    private String lastName;

    @Column("contact_phone")
    private String phone;

    @OneToMany(subTable = "t_company_vendor", joinValue = "company_id", cascadeInsertOrUpdate = true, reversePropertyName = "companyId")
    private List<Vendor> vendorList;

}