具有多个实体的 JPA 连接表

发布于 2024-08-01 14:58:24 字数 902 浏览 8 评论 0原文

我有一个看起来像这样的实体:

public class NpcTradeGood implements Serializable, Negotiabble {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected NpcTradeGoodPK npcTradeGoodPK;
    @JoinColumn(name = "npc_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Npc npc;
}

@Embeddable
public class NpcTradeGoodPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "npc_id", nullable = false)
    private long npcId;
    @Basic(optional = false)
    @Column(name = "good_id", nullable = false)
    private long goodId;
    @Basic(optional = false)
    @Column(name = "type", nullable = false)
    @Enumerated(EnumType.STRING)
    private ItemType type;
}

有没有办法根据类型列(枚举)告诉 JPA 它是哪个 OneToMany 关系? 就像它的一部分或任何其他实体一样,它会自动获取相关实体。

提前致谢。

I have an Entity that looks like this:

public class NpcTradeGood implements Serializable, Negotiabble {
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    protected NpcTradeGoodPK npcTradeGoodPK;
    @JoinColumn(name = "npc_id", referencedColumnName = "id", nullable = false, insertable = false, updatable = false)
    @ManyToOne(optional = false)
    private Npc npc;
}

@Embeddable
public class NpcTradeGoodPK implements Serializable {
    @Basic(optional = false)
    @Column(name = "npc_id", nullable = false)
    private long npcId;
    @Basic(optional = false)
    @Column(name = "good_id", nullable = false)
    private long goodId;
    @Basic(optional = false)
    @Column(name = "type", nullable = false)
    @Enumerated(EnumType.STRING)
    private ItemType type;
}

Is there a way to tell JPA which OneToMany relationship it is based on the type column (enumeration)?
Like if its a part or any other entity it automatically gets the related entity.

Thanks in advance.

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

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

发布评论

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

评论(1

坦然微笑 2024-08-08 14:58:24

在你的 PK 对象中,你不需要将 id 存储为长整型(实际上,每次你需要引用实体时都是如此)。 当映射到实际的数据库模式时,JPA 会将所有对其他实体的引用替换为这些实体的 ID。

因此,如果您使用它(请注意,我将您的“长”id 替换为对实体的实际引用):

@Embeddable
public class NpcTradeGoodPK implements Serializable {
    @ManyToOne
    @JoinColumn(name = "npc_id", nullable = false)
    private Npc npc;
    @ManyToOne
    @JoinColumn(name = "good_id", nullable = false)
    private Good good;
    @Column(name = "type", nullable = false)
    @Enumerated(EnumType.STRING)
    private ItemType type;
}    

... JPA 会将其映射到数据库,使用:“long npc_id”,其中我们指的是“Npc npc”; 和“long good_id”,我们指的是“Good good”。

一件重要的事情:您不能将 @Column 与 @ManyToOne 一起使用。 您可以使用 @JoinColumn 代替,这将允许您执行与现在相同的操作。

此外,您不需要指定所有这些“可选选项”。 'nullable' 应该解决这个问题。

编辑:啊,NpcTradeGoodPK 中的 Npc 可能会与嵌入它的实体中的 Npc 发生碰撞。 考虑重命名其中一个。

In your PK object, you don't need to store the ids as longs (actually, this is true every time you need a reference to an entity). When mapping to the actual DB schema, JPA replaces all the references to other entities by thoes entities' ids.

So, if you use this (notice that I replaced your 'long' ids with actual references to the entities):

@Embeddable
public class NpcTradeGoodPK implements Serializable {
    @ManyToOne
    @JoinColumn(name = "npc_id", nullable = false)
    private Npc npc;
    @ManyToOne
    @JoinColumn(name = "good_id", nullable = false)
    private Good good;
    @Column(name = "type", nullable = false)
    @Enumerated(EnumType.STRING)
    private ItemType type;
}    

... JPA will map this to the DB, using: "long npc_id" where we refer to "Npc npc"; and "long good_id" where we refer to "Good good".

One important thing: you cannot use @Column with @ManyToOne. You may use @JoinColumn instead which will allow you to do the same things you do now.

Also, you don't need to specify all those 'optionals'. 'nullable' should take care of that.

Edited: ah, the Npc in NpcTradeGoodPK will probably collide with the Npc in the entity that embeds it. Consider renaming one of them.

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