supaplexy supaplexy - 3 months ago 26
Java Question

org.hibernate.exception.ConstraintViolationException: could not insert

When I try to add into

TDEPOFAZLA
table, I get the following error:


org.springframework.dao.DataIntegrityViolationException: could not insert: [tr.gov.tcmb.pgmtems.model.DepoFazla]; SQL [insert into PGMTEMS.TDEPOFAZLA (ID, FAZLABULUNDURMAORANI, GRUP) values (default, ?, ?)]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not insert: [tr.gov.tcmb.pgmtems.model.DepoFazla]


Here is my JUnit test function:

@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);
}


Here is my 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
}


Here is 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


When I debug the JUnit test, I get this error:


Error: DB2 SQL Error: SQLCODE=-407, SQLSTATE=23502, SQLERRMC=TBSPACEID=2, TABLEID=75, COLNO=2, DRIVER=3.50.152 SQLState: 23502 ErrorCode: -407


When I search the error, I find that I try to insert NULL but I can't figure out where I add null value.

This is how I create TDEPOFAZLA table:

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);


This is how I create TDEPOTUR table:

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);


Any ideas on what I should do?

Answer

The problem was that I didn't reference the join column properly. This solved the problem:

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "DEPOTUR", referencedColumnName = "ID", nullable = false, columnDefinition = "DECIMAL(20,0)")
@NotNull
private DepoTur depoTur;