【发布时间】:2016-07-15 13:52:35
【问题描述】:
当我尝试添加到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 错误代码:-407
当我搜索错误时,我发现我尝试插入 NULL 但我无法弄清楚我在哪里添加了 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);
对我应该做什么有什么想法吗?
【问题讨论】:
-
你是否为所有属性定义了getter/setter?
-
TDEPOFAZLA有四个不可为空的列,而您的插入语句提供的值较少。表定义中ID没有默认值,DEPOTUR完全缺失。你为什么用 MySQL> 标记这个 -
@cralfaro 我已经为所有属性定义了 getter/setter 并相应地更新了问题。