Reputation: 53
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
Upvotes: 3
Views: 2417
Reputation: 53
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.
Upvotes: 1