将父表的主键映射到子表的外键

发布于 2025-01-19 16:46:33 字数 1019 浏览 0 评论 0原文

我有一个父表,我正在尝试在父表和子表之间建立@Onetomany的关系(例如,一个父表可以与几个子表相关联)。

parent.java

@Entity
@Table(name = "parent")
public class Threat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long parentId;

    @Column(name = "name")
    private String parentName;
}

child.java

@Entity
@Table(name = "child")
public class Child {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long childId;

    @Column(name = "parent_id")
    private long parentId;

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

    @Column(name = "age")
    private Integer childAge;
}

我在弄清楚如何做这件事时遇到了麻烦.parent_id)。理想情况下,当我返回父对象时,我希望同时列出父母ID和显示的孩子列表,因此父级列出了父母和子女的列表。这样,如果父母没有孩子,我仍然有父母ID。

我一直在进行一些研究,我想知道我是否应该使用

@PrimaryKeyJoinColumn 

和/或

@Inheritance(strategy = InheritanceType.JOINED)

无法弄清楚如何实施它们。任何帮助将不胜感激。

I have a parent table, and I am trying to create a @OneToMany relationship between the parent and child tables (e.g. one parent table can be associated with several child tables).

Parent.java

@Entity
@Table(name = "parent")
public class Threat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long parentId;

    @Column(name = "name")
    private String parentName;
}

Child.java

@Entity
@Table(name = "child")
public class Child {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long childId;

    @Column(name = "parent_id")
    private long parentId;

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

    @Column(name = "age")
    private Integer childAge;
}

I am having trouble figuring out how to do this given the fact I need to join the primary key of the parent table (parent.id) to a column in the child table that is not the primary key (child.parent_id). Ideally, when I return a Parent object, I would like both the parent id and the list of children displayed, so the parent id is listed in both the parent and children. That way, if the parent has no children, I still have the parent id in my object.

I have been doing some research, and I'm wondering if I should be using

@PrimaryKeyJoinColumn 

and/or

@Inheritance(strategy = InheritanceType.JOINED)

I haven't been able to figure out how to implement them though. Any help would be greatly appreciated.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

昔梦 2025-01-26 16:46:33

查看@JoinColumn。
来自 Java 文档:

指定用于连接实体关联或元素集合的列。如果 JoinColumn 注释本身是默认的,则假定单个连接列并应用默认值。

示例:

   @ManyToOne
   @JoinColumn(name="ADDR_ID")
   public Address getAddress() { return address; }

示例:使用外键的单向一对多关联
映射

   // In Customer class
   @OneToMany
   @JoinColumn(name="CUST_ID") // join column is in table for Order
   public Set<Order> getOrders() {return orders;}

有了这个,您的实体应该如下所示:

@Entity
@Table(name = "parent")
public class Threat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long parentId;

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

    @OneToMany
    @JoinColumn(name="parent_id")
    private List<Child> children = new ArrayList<>();
}

现在,每当您获取 Parent 时,您都可以访问关联的子实体。

此外,如果您在查询父实体时始终需要提取子实体,则可以指定@OneToMany(fetch = FetchType.EAGER)。

Checkout @JoinColumn.
From Java doc :

Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.

Example:

   @ManyToOne
   @JoinColumn(name="ADDR_ID")
   public Address getAddress() { return address; }

Example: unidirectional one-to-many association using a foreign key
mapping

   // In Customer class
   @OneToMany
   @JoinColumn(name="CUST_ID") // join column is in table for Order
   public Set<Order> getOrders() {return orders;}

With this your entity should look like the following :

@Entity
@Table(name = "parent")
public class Threat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long parentId;

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

    @OneToMany
    @JoinColumn(name="parent_id")
    private List<Child> children = new ArrayList<>();
}

Now whenever you fetch Parent , you can access the associated child entities.

Additionally you can specify @OneToMany(fetch = FetchType.EAGER) if you always need child entities fetched whenever you query for parent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文