当我尝试添加到TDEPOFAZLA
表时,出现以下错误:
org.springframework.dao.DataIntegrityViolationException:无法插入:[tr.gov.tcmb.pgmtems.model.DepoFazla]; SQL [插入PGMTEMS.TDEPOFAZLA(ID,FAZLABULUNDURMAORANI,GRUP)值(默认值,?,?)];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法插入:[tr.gov.tcmb.pgmtems.model.DepoFazla]
这是我的 JUnit 测试功能:
@Test
public void testSaveDepoFazla() {
DepoTur depoTur = new DepoTur("my tür", 5);
depoTurService.saveDepoTur(depoTur);
List<DepoTur> list = depoTurService.getDepoTurList();
assertNotNull(list.get(0));
BigDecimal fazlaBulundurmaOrani = new BigDecimal(6000);
DepoFazla depoFazla = new DepoFazla(1, list.get(0), fazlaBulundurmaOrani);
depoFazlaService.saveDepoFazla(depoFazla);
}
这是我的 DepoFazla.java :
@Entity
@Table(schema = "PGMTEMS", name = "TDEPOFAZLA")
public class DepoFazla implements Serializable {
private static final long serialVersionUID = -2800365387332643658L;
@Id
@GeneratedValue
@Column(name = "ID", nullable = false, updatable = false)
private Long id;
@Column(name = "GRUP", nullable = false, columnDefinition = "INTEGER")
private Integer grup;
@ManyToOne(fetch = FetchType.LAZY, targetEntity = DepoTur.class)
@JoinColumn(name = "ID", insertable = false, updatable = false)
@NotNull
private DepoTur depoTur;
@Column(name = "FAZLABULUNDURMAORANI", nullable = false, columnDefinition = "DECIMAL(6, 2)")
private BigDecimal fazlaBulundurmaOrani;
public DepoFazla() {
super();
}
public DepoFazla(Integer grup, DepoTur depoTur, BigDecimal fazlaBulundurmaOrani) {
super();
this.grup = grup;
this.depoTur = depoTur;
this.fazlaBulundurmaOrani = fazlaBulundurmaOrani;
}
//GETTER AND SETTER METHODS
}
这是 DepoTur.java :
@Entity
@Table(schema = "PGMTEMS", name = "TDEPOTUR")
public class DepoTur implements Serializable {
private static final long serialVersionUID = 6203672609079710060L;
@Id
@GeneratedValue
@Column(name = "ID", nullable = false, updatable = false)
@Index(name = "XUTDEPOTURP", columnNames = { "id" })
private Long id;
@Column(name = "ACIKLAMA", nullable = false)
private String aciklama;
@Column(name = "BLOKESIRASI", nullable = false)
private Integer blokeSirasi; //
@Column(name = "DEPOCINSI")
private String depoCinsi;
public DepoTur() {
super();
}
public DepoTur(String aciklama, Integer blokeSirasi, String depoCinsi) {
super();
this.aciklama = aciklama;
this.depoCinsi = depoCinsi;
this.blokeSirasi = blokeSirasi;
}
public DepoTur(String aciklama, Integer blokeSirasi) {
super();
this.aciklama = aciklama;
this.blokeSirasi = blokeSirasi;
}
//GETTER AND SETTER METHODS
当我调试JUnit测试时,我收到此错误:
错误:DB2 SQL错误:SQLCODE = -407,SQLSTATE = 23502,SQLERRMC = TBSPACEID = 2,TABLEID = 75,COLNO = 2,DRIVER = 3.50.152 SQLState:23502 ErrorCode:-407
当我搜索错误时,我发现我尝试插入NULL,但我无法弄清楚我在哪里添加空值。
这是我创建 TDEPOFAZLA 表格的方式:
CREATE TABLE TDEPOFAZLA
(
ID decimal(20,0) PRIMARY KEY NOT NULL,
GRUP int NOT NULL,
DEPOTUR decimal(20,0) NOT NULL,
FAZLABULUNDURMAORANI decimal(6,2) NOT NULL
);
CREATE UNIQUE INDEX XUTDEPOFAZLAP ON TDEPOFAZLA(ID);
这是我创建 TDEPOTUR 表格的方式:
CREATE TABLE TDEPOTUR
(
ID decimal(20,0) PRIMARY KEY NOT NULL,
ACIKLAMA varchar(100) NOT NULL,
DEPOCINSI char(1),
BLOKESIRASI int NOT NULL
);
CREATE UNIQUE INDEX XUTDEPOTURP ON TDEPOTUR(ID);
关于我应该做什么的任何想法?
答案 0 :(得分:0)
您似乎在 DepoFazla
模型中为连接列ID指定了两次相同的名称,您必须更改其名称使用,例如:
@Entity
@Table(schema = "PGMTEMS", name = "TDEPOFAZLA")
public class DepoFazla implements Serializable {
private static final long serialVersionUID = -2800365387332643658L;
...
@ManyToOne(fetch = FetchType.LAZY, targetEntity = DepoTur.class)
@JoinColumn(name = "ID_depoTur", insertable = false, updatable = false)
@NotNull
private DepoTur depoTur;
...
}
答案 1 :(得分:0)
使用FK的属性定义是错误的,因为我看到你使用可更新,可插入到false,你真正想要的是FK对象没有被修改,因为它可能是一些其他实体共用的主表。
然后您可以使用的是
@ManyToOne(cascade= {CascadeType.DETACH})
@JoinColumn(name = "DEPOTUR")
@NotNull
private DepoTur depoTur;
使用DETACH,您只会将值保存在第一个表DEPOTUR列中,并且不会更新DepoTur表中的对象
同时在第一张表中添加FK
CREATE TABLE TDEPOFAZLA
(
ID decimal(20,0) PRIMARY KEY NOT NULL,
GRUP int NOT NULL,
DEPOTUR decimal(20,0) NOT NULL,
FAZLABULUNDURMAORANI decimal(6,2) NOT NULL
);
CREATE UNIQUE INDEX XUTDEPOFAZLAP ON TDEPOFAZLA(ID);
CONSTRAINT fk_column FOREIGN KEY (DEPOTUR) REFERENCES TDEPOTUR(ID);
答案 2 :(得分:0)
问题是我没有正确引用连接列。这解决了这个问题:
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "DEPOTUR", referencedColumnName = "ID", nullable = false, columnDefinition = "DECIMAL(20,0)")
@NotNull
private DepoTur depoTur;