复合主键和外键jpa(Composite Primary and foreign key jpa)

编程入门 行业动态 更新时间:2024-10-22 14:28:21
复合主键和外键jpa(Composite Primary and foreign key jpa)

我有两张桌子T1和T2。 T1有id(PK),名称,类型。 T2有SID,TID,Type。 T2的主键是所有3的组合:SID,TID,Type。 现在SID和TID也是映射到T1的ID的外键。 有人请帮助如何在JPA中做到这一点。

package com.sap.table; import java.io.Serializable; import java.lang.String; import javax.persistence.*; import static javax.persistence.GenerationType.TABLE; import static javax.persistence.GenerationType.IDENTITY; /** * Entity implementation class for Entity: Relations * */ @IdClass(RelationsPK.class) @Entity @Table (name = "demo_relations11") public class Relations implements Serializable { //@EmbeddedId //@JoinColumn(referencedColumnName="AssetID") //private RelationsPK Relations_PK; // @Id //// @JoinColumn(referencedColumnName="AssetID") // private String TargetID; @Id private String Type; private static final long serialVersionUID = 1L; // @MapsId("SourceID") @Id @ManyToOne(targetEntity=Asset.class) @JoinColumns({ @JoinColumn(name="SourceID", referencedColumnName="AssetID"), @JoinColumn(name="TargetID", referencedColumnName="AssetID") }) //@JoinColumn(referencedColumnName="AssetID") Asset asset; public Asset getAsset() { return asset; } public void setAsset(Asset asset) { this.asset = asset; } public Relations() { super(); } // public String getSourceID() { // return this.SourceID; // } // // public void setSourceID(String SourceID) { // this.SourceID = SourceID; // } // public String getTargetID() { // return this.TargetID; // } // // public void setTargetID(String TargetID) { // this.TargetID = TargetID; // } public String getType() { return this.Type; } public void setType(String Type) { this.Type = Type; } }

T1 - ID(PK),名称,类型T2 - SID(FK到ID),TID(FK到ID),T2的类型PK - SID,TID,类型

I have two tables T1 and T2. T1 has id(PK), name , type. T2 has SID, TID, Type. Primary Key for T2 is combination of all 3: SID, TID, Type. Now SID and TID are foreign keys also mapped to ID of T1. Someone please help how to make this in JPA.

package com.sap.table; import java.io.Serializable; import java.lang.String; import javax.persistence.*; import static javax.persistence.GenerationType.TABLE; import static javax.persistence.GenerationType.IDENTITY; /** * Entity implementation class for Entity: Relations * */ @IdClass(RelationsPK.class) @Entity @Table (name = "demo_relations11") public class Relations implements Serializable { //@EmbeddedId //@JoinColumn(referencedColumnName="AssetID") //private RelationsPK Relations_PK; // @Id //// @JoinColumn(referencedColumnName="AssetID") // private String TargetID; @Id private String Type; private static final long serialVersionUID = 1L; // @MapsId("SourceID") @Id @ManyToOne(targetEntity=Asset.class) @JoinColumns({ @JoinColumn(name="SourceID", referencedColumnName="AssetID"), @JoinColumn(name="TargetID", referencedColumnName="AssetID") }) //@JoinColumn(referencedColumnName="AssetID") Asset asset; public Asset getAsset() { return asset; } public void setAsset(Asset asset) { this.asset = asset; } public Relations() { super(); } // public String getSourceID() { // return this.SourceID; // } // // public void setSourceID(String SourceID) { // this.SourceID = SourceID; // } // public String getTargetID() { // return this.TargetID; // } // // public void setTargetID(String TargetID) { // this.TargetID = TargetID; // } public String getType() { return this.Type; } public void setType(String Type) { this.Type = Type; } }

T1 - ID(PK), Name, Type T2 - SID (FK to ID), TID (FK to ID), Type PK of T2 - SID,TID,Type

最满意答案

我找到了解决方案。 首先,我使用了@IdClass表示法。 这是主键的另一个类。 然后使用@Id在我的类中进行注释以进行注释。 我也用过

@JoinColumn(name = "") @ManyToOne(targetEntity = parent_class)

我对引用父表的所有主键使用了上述注释。

I have found the solution. First of all i used @IdClass Notation. That is made another class for the primary key. Then used @Id for annotating in my class to annotated. I also used

@JoinColumn(name = "") @ManyToOne(targetEntity = parent_class)

I used the above annotation for all the primary keys referencing the parent table.

更多推荐

本文发布于:2023-04-29 11:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336315.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   jpa   Composite   key   foreign

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!