简体   繁体   English

当子实体与父实体之间具有一对一的JPA关系时,可以持久保留子实体和父实体吗?

[英]Is it ok to persist the Child entity along with the Parent entity when having a One-To-One JPA relationship in between them?

I am having two Entity classes, Company and CompanyTypeAutomobile . 我有两个实体类, CompanyCompanyTypeAutomobile
These two have One-To-One relationships. 这两个具有One-To-One关系。

public class Company {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "company_id", unique = true, nullable = false)
    private Integer companyId;

    @Column(name = "name", length = 255, updatable = false)
    @NotEmpty
    private String name;

    private CompanyType companyType;

    //other fields

}

public class CompanyTypeAutomobile {

    @Id
    private Integer id;

    @JsonIgnore
    @OneToOne(fetch = FetchType.LAZY)
    @MapsId
    @JoinColumn(name = "company_id")
    private Company company;

}

Company contains the basic details of a company, and the other fields would be contained in its child entity(based on the type of Company) Now, when If Company is created, should I create the CompanyTypeAutomobile also in the same process? Company包含了公司的基本信息,以及其他领域会包含在它的子实体(基于公司的类型)。现在,如果在Company被创建,我应该创建CompanyTypeAutomobile也以相同的过程?

I am having a requirement where I am in such a situation. 我在这种情况下有一个要求。 If for some company, it has only basic details in Company and other details were not created and I want GET the complete details of the company then the persistence layer would return me EntityNotFoundException for CompanyTypeAutomobile . 如果某家公司,它在只有基本信息Company及其他细节尚未建立,我希望得到公司的完整细节那么持久层将返回我EntityNotFoundExceptionCompanyTypeAutomobile

So I just want to create the child entity, only with the id field. 因此,我只想创建带有id字段的子实体。 Is it a good practice? 这是一个好习惯吗?

Now, when If Company is created, should I create the CompanyTypeAutomobile also in the same process? 现在,如果创建了CompanyTypeAutomobile ,我是否也应该在相同的过程中创建CompanyTypeAutomobile

It depends on whether the child entity is mandatory or not. 这取决于子实体是否是强制性的。

When you load the child entity using the Company identifier: 使用Company标识符加载子实体时:

CompanyTypeAutomobile details = entityManager.find(CompanyTypeAutomobile.class, company.getId());

you will get null if there is no CompanyTypeAutomobile in the DB, not an EntityNotFoundException . 如果数据库中没有CompanyTypeAutomobile而不是EntityNotFoundException ,则将为null

So I just want to create the child entity, only with the id field. 因此,我只想创建带有id字段的子实体。 Is it a good practice? 这是一个好习惯吗?

No, it's not. 不,这不对。 That would be a hack. 那将是一个hack。

If you get an EntityNotFoundException , it means you mapped the child association in the parent entity, which is a mistake . 如果获得EntityNotFoundException ,则意味着您已将子关联映射到父实体中,这是一个错误

In that case, remove the CompanyTypeAutomobile association in the parent Company entity as it will behave according to the EAGER fetching strategy and lead to N+1 query issues . 在这种情况下,请删除父Company实体中的CompanyTypeAutomobile关联,因为它会根据EAGER提取策略进行操作,并导致N + 1查询问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM