Hello Everyone,
I have the following database tables:
create table "SAPN73DB"."OEE_KPI"(
"CLIENT" VARCHAR (3) UNICODE not null,
"KPI" VARCHAR (20) UNICODE not null,
"KPI_TYPE" VARCHAR (50) UNICODE,
"UOM_OF_TARGETS" VARCHAR (50) UNICODE,
constraint SYSPRIMARYKEY primary key ("CLIENT","KPI"))
create table "SAPN73DB"."OEE_KPIT"(
"CLIENT" VARCHAR (3) UNICODE not null,
"KPI" VARCHAR (20) UNICODE not null,
"LANG" VARCHAR (2) UNICODE not null,
"DESCRIPTION" VARCHAR (50) UNICODE,
constraint SYSPRIMARYKEY primary key ("CLIENT","KPI","LANG"))
The "OEE_KPI" is the parent table and "OEE_KPIT" is the child table, so there is a one to many relationship. The Join columns are "CLIENT" and "KPI".
Hence I created the corresponding JPA classes:
- @Entity
- @Table(name = "OEE_KPI")
- public class KPI implements Serializable {
- private static final long serialVersionUID = 1L;
-
- @EmbeddedId
- private KPIPK id;
- @Column(name = "KPI_TYPE")
- private String kpiType;
- @Column(name = "UOM_OF_TARGETS")
- private String uomOfTargets;
-
-
- @OneToMany(mappedBy = "oeeKpi", cascade = CascadeType.ALL)
- private List<KPIText> KPIText;
-
- public KPI() {
- }
-
- public String getKpiType() {
- return this.kpiType;
- }
- public void setKpiType(String kpiType) {
- this.kpiType = kpiType;
- }
- public String getUomOfTargets() {
- return this.uomOfTargets;
- }
- public void setUomOfTargets(String uomOfTargets) {
- this.uomOfTargets = uomOfTargets;
- }
- public KPIPK getId() {
- return id;
- }
- public void setId(KPIPK id) {
- this.id = id;
- }
- public List<KPIText> getKPIText() {
- return KPIText;
- }
- public void setKPIText(List<KPIText> kPIText) {
- KPIText = kPIText;
- }
- }
- @Entity
- @Table(name = "OEE_KPIT")
- public class KPIText implements Serializable{
-
- private static final long serialVersionUID = 1L;
-
- @EmbeddedId
- private KPITextPK id;
- @Column(name="DESCRIPTION")
- private String description;
-
-
- @ManyToOne(optional=false,cascade = CascadeType.ALL)
- @JoinColumns({
- @JoinColumn(name="CLIENT", referencedColumnName="CLIENT"),
- @JoinColumn(name="KPI", referencedColumnName="KPI")
- })
- private KPI oeeKpi;
-
- public KPIText() {
- }
- public String getDescription() {
- return this.description;
- }
- public void setDescription(String description) {
- this.description = description;
- }
-
- public KPI getOeeKpi() {
- return this.oeeKpi;
- }
- public void setOeeKpi(KPI oeeKpi) {
- this.oeeKpi = oeeKpi;
- }
- public KPITextPK getId() {
- return id;
- }
- public void setId(KPITextPK id) {
- this.id = id;
- }
- }
================================================================
- @Embeddable
- public class KPIPK implements Serializable {
-
- private static final long serialVersionUID = 1L;
- private String client;
- private String kpi;
-
- public KPIPK() {
- }
- public String getClient() {
- return this.client;
- }
- public void setClient(String client) {
- this.client = client;
- }
- public String getKpi() {
- return this.kpi;
- }
- public void setKpi(String kpi) {
- this.kpi = kpi;
- }
-
- public boolean equals(Object other) {
- if (this == other) {
- return true;
- }
- if (!(other instanceof KPIPK)) {
- return false;
- }
- KPIPK castOther = (KPIPK)other;
- return
- this.client.equals(castOther.client)
- && this.kpi.equals(castOther.kpi);
-
- }
-
- public int hashCode() {
- final int prime = 31;
- int hash = 17;
- hash = hash * prime + this.client.hashCode();
- hash = hash * prime + this.kpi.hashCode();
-
- return hash;
- }
- }
================================================================
- @Embeddable
- public class KPITextPK implements Serializable {
-
- private static final long serialVersionUID = 1L;
- private String client;
- private String kpi;
-
- @Column(name="LANG")
- private String language;
-
- public KPITextPK() {
- }
- public String getClient() {
- return this.client;
- }
- public void setClient(String client) {
- this.client = client;
- }
- public String getKpi() {
- return this.kpi;
- }
- public void setKpi(String kpi) {
- this.kpi = kpi;
- }
- public String getLanguage() {
- return this.language;
- }
- public void setLanguage(String language) {
- this.language = language;
- }
-
- public boolean equals(Object other) {
- if (this == other) {
- return true;
- }
- if (!(other instanceof KPITextPK)) {
- return false;
- }
- KPITextPK castOther = (KPITextPK)other;
- return
- this.client.equals(castOther.client)
- && this.kpi.equals(castOther.kpi)
- && this.language.equals(castOther.language);
-
- }
-
- public int hashCode() {
- final int prime = 31;
- int hash = 17;
- hash = hash * prime + this.client.hashCode();
- hash = hash * prime + this.kpi.hashCode();
- hash = hash * prime + this.language.hashCode();
-
- return hash;
- }
- }
================================================================
Now when I execute the following code:
- ArrayList<KPIText> kpiTextList = new ArrayList<KPIText>();
- KPIText kpiText1 = new KPIText();
- KPITextPK textId = new KPITextPK();
- kpiText1.setId(textId);
- kpiText1.getId().setClient("006");
- kpiText1.setDescription("Availability Description English");
- kpiText1.getId().setKpi("Availability");
- kpiText1.getId().setLanguage("EN");
- kpiTextList.add(kpiText1);
-
- KPIText kpiText2 = new KPIText();
- KPITextPK textId2 = new KPITextPK();
- kpiText2.setId(textId2);
- kpiText2.getId().setClient("006");
- kpiText2.setDescription("Availability Description Deutsche");
- kpiText2.getId().setKpi("Availability");
- kpiText2.getId().setLanguage("DE");
- kpiTextList.add(kpiText2);
-
- KPI kpiHeader = new KPI();
- KPIPK kpiId = new KPIPK();
- kpiHeader.setId(kpiId);
- kpiHeader.getId().setClient("006");
- kpiHeader.getId().setKpi("Availability");
- kpiHeader.setKpiType("Positive");
- kpiHeader.setUomOfTargets("Percentage");
- kpiHeader.setKPIText(kpiTextList);
-
- em.persist(kpiHeader);
I am getting an error saying:
Caused by: com.sap.sql.log.OpenSQLException: The SQL statement "INSERT INTO "OEE_KPIT" ("CLIENT","KPI","LANG","DESCRIPTION","CLIENT","KPI") VALUES (?,?,?,?,?,?)" contains the semantics error[s]: - 1:61 - column ""CLIENT"" not unique
- 1:70 - column ""KPI"" not unique
Can anyone help me why the "CLIENT" and "KPI" field is added again in the insert statement? Kindly help me in fixing this error.
Regards,
Anil.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.