Hibernate外键冗余调用

问题描述 投票:0回答:1

我有一个休眠实体类

public class Category {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String description;
    private Boolean visible;

    @ManyToOne
    @JoinColumn(name = "parent_id")
    private Category parent;

    @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
    @Builder.Default
    private List<Category> children = new ArrayList<>();

    private UUID budgetId;
}

我正在尝试更新它的数据,包括父级。我只有这个父项的 id,因为在表中我们有parent_id,通过 id 调用完整父项以仅将其设置在实体上是愚蠢的。您有什么想法可以避免对数据库的冗余调用吗?

spring hibernate orm
1个回答
0
投票

您可以只使用“存根”实体。

Category parent= new Category();
parent.setId(parentId);
category.setParent(parent);
© www.soinside.com 2019 - 2024. All rights reserved.