具有多个实体的 JPA 连接表
我有一个看起来像这样的实体:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在你的 PK 对象中,你不需要将 id 存储为长整型(实际上,每次你需要引用实体时都是如此)。 当映射到实际的数据库模式时,JPA 会将所有对其他实体的引用替换为这些实体的 ID。
因此,如果您使用它(请注意,我将您的“长”id 替换为对实体的实际引用):
... 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):
... 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.