Spring Data JPA 仅供专业人士使用

发布于 2025-01-09 09:10:21 字数 1824 浏览 1 评论 0原文

我的目标是从单个实体(Box)创建多个实体(Subbox),就像 俄罗斯套娃。 为了抽象我们在这里要解决的问题,请考虑以下场景:

首先我们有一个实体“Box”:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@ToString
@Table(name="box")
public class Box {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idbox", updatable = false, nullable = false)
    private int idbox;

    @Column(name = "brand")
    private String brand;

    @Column(name = "size")
    private BigDecimal size;

    @OneToMany
    private Set<Subbox> subbox;

    @OneToMany
    private Set<Content> content;
}

和另一个实体 Subbox:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@ToString
@Table(name="subbox")
public class Subbox {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idsubbox", updatable = false, nullable = false)
    private int idsubbox;

    @Column(name = "brand")
    private String brand;

    @Column(name = "size")
    private BigDecimal size;

    @ManyToOne
    @JoinColumn
    private Box box;

    @OneToMany
    private Set<Content> content;
}

如果我们从 Subbox 查询,我们应该得到类似的结果:

+----------+-------+------+-------------------+-----------+
| idsubbox | brand | size | content_idcontent | box_idbox |
+----------+-------+------+-------------------+-----------+
  1          Abcd    5.0     1                  13
  2          Abcd    5.0     1                  13
  3          Abcd    5.0     1                  13

所以我们有 3 个不同的子框(1, 2、3)同一个盒子(13)。问题是,使用上面的配置,不可能在子框中继续创建子框,我们必须创建一个 Subsubbox 实体、Subsubsubbox 等等。

问题是,如何在一个 Box 内无限地创建一个 Box,而不需要创建 N 个实体? 内部盒子(顶部盒子内的盒子)应具有与顶部盒子完全相同的属性,但其值可能不同,就像娃娃具有不同的尺寸一样。

My goal is to create multiple entities (Subbox) from a single entity (Box), just like
Matryoshka dolls.
To abstract what we are addressing here, consider the following scenario:

First we have an entity "Box":

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@ToString
@Table(name="box")
public class Box {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idbox", updatable = false, nullable = false)
    private int idbox;

    @Column(name = "brand")
    private String brand;

    @Column(name = "size")
    private BigDecimal size;

    @OneToMany
    private Set<Subbox> subbox;

    @OneToMany
    private Set<Content> content;
}

and another entity Subbox:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@ToString
@Table(name="subbox")
public class Subbox {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "idsubbox", updatable = false, nullable = false)
    private int idsubbox;

    @Column(name = "brand")
    private String brand;

    @Column(name = "size")
    private BigDecimal size;

    @ManyToOne
    @JoinColumn
    private Box box;

    @OneToMany
    private Set<Content> content;
}

If we query from Subbox, we should get something similar to that:

+----------+-------+------+-------------------+-----------+
| idsubbox | brand | size | content_idcontent | box_idbox |
+----------+-------+------+-------------------+-----------+
  1          Abcd    5.0     1                  13
  2          Abcd    5.0     1                  13
  3          Abcd    5.0     1                  13

So we have 3 different subboxes(1, 2, 3) of the same box(13). The problem is that with the configuration above it's not possible to keep creating subboxes inside a subbox, we would have to create a Subsubbox entity, Subsubsubbox and so on.

The question is, how can I create a Box inside a Box infinitely without having to create N entities?
An inner box (the box inside a top box) should have the very same properties from the top box, but its values could differ, just like the dolls have different sizes.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文