我有两张桌子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.
更多推荐
发布评论