JPA:多态关联和联接表

时间:2018-09-18 18:09:43

标签: java hibernate jpa

我对Hibernate和JPA还是陌生的。我有一个Identity类,它与EntityInformation具有一对一的关系,该子类可归类为PersonalInformation或CompanyInformation。

我正在尝试使用Joined表策略来保持DRY状态,以便数据库中的基本EntityInformation表具有公共字段,而PersonalInformation和CompanyInformation表仅具有特定于类的字段

当我创建带有“公司”类型的标识时,我想为该标识创建一个CompanyInformation。我遇到的问题是,当我创建一个身份时,EntityInformation会保留,而不是Personal / CompanyInformation。

这可能吗?我觉得我缺少了一些东西或需要对模型进行不同的建模。任何帮助将不胜感激!


这是我的身份课程:

@Entity
@Table(name = "identities")

public class Identity {

  @NotNull
  @Enumerated(EnumType.STRING)
  // type is either Personal or Company
  private IdentityType type;

  @NotNull
  @OneToOne(
    mappedBy = "identity", cascade = CascadeType.ALL, orphanRemoval = true, optional = false)

  private EntityInformation entityInformation;
  ...
}

EntityInformation类:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name = "entity_informations")

public class EntityInformation {

  @NotNull private Boolean hasTaxPayerId;

  @OneToOne(optional = false)
  @JoinColumn(name = "identity_id", nullable = false)
  private Identity identity;
  ...    
}

PersonalInformation类:

public class PersonalInformation extends EntityInformation{

  @NotBlank private String firstName;

  @NotBlank private String lastName;

  private String middleName;
  ...
}

CompanyInformation类:

public class CompanyInformation extends EntityInformation{

  @NotBlank private String name;
  ...
}

2 个答案:

答案 0 :(得分:1)

您的身份表有点混乱。它与实体具有一对一关系,并指定实体类型。在设计时,最好对架构进行非规范化并将类型保留在实体中。

优点:更好的性能(而不是额外的联接),清晰度和级联问题的开销更少。

您可以在EntityInformation中添加具有类型的新字段,并在子实体中定义它(如果需要)。

答案 1 :(得分:0)

外观不错,但我看不到ids。您应该删除optional = false上的Identity。该属性仅在此处可供检索,因为EntityInformation是关系的所有者,因此Identity模式中未放置任何内容。这会导致鸡肉和鸡蛋问题b / c,您不能同时创建两个新实体并将其保存在同一事务中b / c都不能为null,但必须先保存另一个。我对其进行了测试,并为我同时保留了EntityInformationCorporateInformation

@Entity
public class Identity {
    @Id @GeneratedValue
    private Long id;

    @OneToOne(mappedBy = "identity", cascade = CascadeType.ALL, orphanRemoval = true)
    private InformationBase information;

@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class InformationBase {
    @Id @GeneratedValue
    private Long id;

    @OneToOne(optional = false)
    @JoinColumn(name = "identity_id", nullable = false)
    private Identity identity;

@Entity
public class CorporateInformation extends InformationBase {

并使用它:

tx.begin();

Identity identity = new Identity();
CorporateInformation corporateInformation = new CorporateInformation();
corporateInformation.setIdentity(identity);
em.persist(identity);
em.persist(corporateInformation);

tx.commit();

显示在日志中

Hibernate: create table CorporateInformation (id bigint not null, primary key (id))
Hibernate: create table Identity (id bigint not null, primary key (id))
Hibernate: create table InformationBase (id bigint not null, identity_id bigint not null, primary key (id))
Hibernate: create table PersonalInformation (id bigint not null, primary key (id))
Hibernate: alter table InformationBase drop constraint if exists UK_s2ny1w2e3fpckgv97n4bhe49h
Hibernate: alter table InformationBase add constraint UK_s2ny1w2e3fpckgv97n4bhe49h unique (identity_id)
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: alter table CorporateInformation add constraint FKq69d75va3x785scp4iki8kprs foreign key (id) references InformationBase
Hibernate: alter table InformationBase add constraint FK9g3vjjvp7ohn3dfirh6u8mwrx foreign key (identity_id) references Identity
Hibernate: alter table PersonalInformation add constraint FK6muqauf869dw0x9jb7jlhcpwo foreign key (id) references InformationBase
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Hibernate: insert into Identity (id) values (?)
Hibernate: insert into InformationBase (identity_id, id) values (?, ?)
Hibernate: insert into CorporateInformation (id) values (?)