使用JPA添加复合外键

时间:2016-08-03 16:35:06

标签: java oracle jpa foreign-keys composite-key

我有这样的代码

@ManyToOne
@ForeignKey(name = "FK_RF_REPLEG")
@JoinColumns({
        @JoinColumn(name="CD_COTIEN", referencedColumnName = "CD_COTIEN", insertable=false, updatable = false),
        @JoinColumn(name="CD_CODENT", referencedColumnName = "CD_CODENT", insertable=false, updatable = false),
        @JoinColumn(name="CD_TIPOID", referencedColumnName = "CD_TIPOID", insertable=false, updatable = false),
        @JoinColumn(name="NR_IDENTI", referencedColumnName = "NR_IDENTI", insertable=false, updatable = false),
        @JoinColumn(name="FC_INICIAL", referencedColumnName = "FC_INICIAL", insertable=false, updatable = false)
})
public RepresentanteLegal getRepresentanteLegal() {
    return representanteLegal;
}

此代码尝试执行下一个SQL查询:

alter table RNREPRES_FACULTAD add constraint FK_RF_REPLEG foreign key (CD_CODENT, CD_COTIEN, CD_TIPOID, FC_INICIAL, NR_IDENTI) references RNREPLEG_INT

出现下一个错误:

02267. 00000 -  "column type incompatible with referenced column type"

之后,我意识到当我直接在我的数据库中执行查询时,查询中的列顺序不同,如下所示:

alter table RNREPRES_FACULTAD add constraint FK_RF_REPLEG foreign key (CD_COTIEN, CD_CODENT, CD_TIPOID, NR_IDENTI, FC_INICIAL) references RNREPLEG_INT

此查询效果非常好。

我的问题是我不知道在哪里更改连接列的顺序以从JPA获取正确的查询。

1 个答案:

答案 0 :(得分:0)

我无法重现你的问题。 这是我的代码:

@MappedSuperclass
public class ABaseEntity {

private int id;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}
}

RepresentanteLegal.class

@Entity
public class RepresentanteLegal extends ABaseEntity {

private String cotien;

private String codent;

private String tipoId;

private String identi;

private String inicial;

@Column(name = "CD_COTIEN")
public String getCotien() {
    return cotien;
}

public void setCotien(String cotien) {
    this.cotien = cotien;
}

@Column(name = "CD_CODENT")
public String getCodent() {
    return codent;
}

public void setCodent(String codent) {
    this.codent = codent;
}

@Column(name = "CD_TIPOID")
public String getTipoId() {
    return tipoId;
}

public void setTipoId(String tipoId) {
    this.tipoId = tipoId;
}

@Column(name = "NR_IDENTI")
public String getIdenti() {
    return identi;
}

public void setIdenti(String identi) {
    this.identi = identi;
}

@Column(name = "FC_INICIAL")
public String getInicial() {
    return inicial;
}

public void setInicial(String inicial) {
    this.inicial = inicial;
}
}

Student.class

@Entity
public class Student extends ABaseEntity {

private RepresentanteLegal representanteLegal;

@ManyToOne
@JoinColumns(value = {
        @JoinColumn(name = "CD_COTIEN", referencedColumnName = "CD_COTIEN", insertable = false, updatable = false),
        @JoinColumn(name = "CD_CODENT", referencedColumnName = "CD_CODENT", insertable = false, updatable = false),
        @JoinColumn(name = "CD_TIPOID", referencedColumnName = "CD_TIPOID", insertable = false, updatable = false),
        @JoinColumn(name = "NR_IDENTI", referencedColumnName = "NR_IDENTI", insertable = false, updatable = false),
        @JoinColumn(name = "FC_INICIAL", referencedColumnName = "FC_INICIAL", insertable = false, updatable = false)
},
        foreignKey =
        @ForeignKey(name = "FK_RF_REPLEG")

)
public RepresentanteLegal getRepresentanteLegal() {
    return representanteLegal;
}

public void setRepresentanteLegal(RepresentanteLegal representanteLegal) {
    this.representanteLegal = representanteLegal;
}
}

该表在oracle数据库中生成正常。这是脚本

  CREATE TABLE "SA"."STUDENT" 
   (    "ID" NUMBER(10,0) NOT NULL ENABLE, 
"CD_COTIEN" VARCHAR2(255 CHAR), 
"CD_CODENT" VARCHAR2(255 CHAR), 
"CD_TIPOID" VARCHAR2(255 CHAR), 
"NR_IDENTI" VARCHAR2(255 CHAR), 
"FC_INICIAL" VARCHAR2(255 CHAR), 
 PRIMARY KEY ("ID")
  USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
    TABLESPACE "USERS"  ENABLE, 
     CONSTRAINT "FKNP277C0W7SX7YJO90YR7MB2NL" FOREIGN KEY ("CD_COTIEN",             "CD_CODENT", "CD_TIPOID", "NR_IDENTI", "FC_INICIAL")
      REFERENCES "SA"."REPRESENTANTELEGAL" ("CD_COTIEN", "CD_CODENT", "CD_TIPOID", "NR_IDENTI", "FC_INICIAL") ENABLE
     ) SEGMENT CREATION DEFERRED 
    PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
   NOCOMPRESS LOGGING
    TABLESPACE "USERS" ;


 CREATE TABLE "SA"."REPRESENTANTELEGAL" 
   (    "ID" NUMBER(10,0) NOT NULL ENABLE, 
"CD_CODENT" VARCHAR2(255 CHAR), 
"CD_COTIEN" VARCHAR2(255 CHAR), 
"NR_IDENTI" VARCHAR2(255 CHAR), 
"FC_INICIAL" VARCHAR2(255 CHAR), 
"CD_TIPOID" VARCHAR2(255 CHAR), 
 PRIMARY KEY ("ID")
   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
   TABLESPACE "USERS"  ENABLE, 
     CONSTRAINT "UK_8PNGU2NOTPR3LP2QVS664T6Y9" UNIQUE ("CD_COTIEN", "CD_CODENT", "CD_TIPOID", "NR_IDENTI", "FC_INICIAL")
   USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 121 COMPUTE STATISTICS 
   TABLESPACE "USERS"  ENABLE
    ) SEGMENT CREATION DEFERRED 
   PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 
  NOCOMPRESS LOGGING
   TABLESPACE "USERS" ;