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?

3

3 Answers

Your property definition with the FK is wrong, as i saw you use updatable, insertable to false, you what really want is that the FK object is not modified because could be a master table common for some other entities.

Then what you can use is this

@ManyToOne(cascade= {CascadeType.DETACH}) @JoinColumn(name = "DEPOTUR") @NotNull private DepoTur depoTur; 

With DETACH, you will save the value only in the first table, column DEPOTUR and wont update the object in the DepoTur table

Also add the FK in your first 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); CONSTRAINT fk_column FOREIGN KEY (DEPOTUR) REFERENCES TDEPOTUR(ID); 
2

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; 

It appears that you specified the same name for the join column Id twice in your DepoFazla model you have to change its name use for example :

@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; ... } 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.