I want to insert in database the entity "CategorieTresorerie" having two join columns. The problem is, one of them (RegroupementCategorieTresorerie) is trying to insert automatically... But this one is already in database, so i have a duplicate key value violation exception.
Error code:
Hibernate: insert into "RegroupementCategorieTresorerie" ("RegroupementCategorieTresorerieCode") values (?)
2015-12-29 15:32:05.207 WARN 6960 --- [tomcat-http--26] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23505
2015-12-29 15:32:05.208 ERROR 6960 --- [tomcat-http--26] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: duplicate key value violates unique constraint "RegroupementCategorieTresorerie_PK"
Détail : Key ("RegroupementCategorieTresorerieCode")=(MARGE_BRUT) already exists.
2015-12-29 15:32:05.210 INFO 6960 --- [tomcat-http--26] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [RegroupementCategorieTresorerie_PK]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
Class I want to insert:
@Entity
public class CategorieTresorerie
{
@Id
@Column(name="CategorieTresorerieID")
@SequenceGenerator(name = "CategorieTresorerie_CategorieTresorerieID_seq", sequenceName = "\"CategorieTresorerie_CategorieTresorerieID_seq\"", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CategorieTresorerie_CategorieTresorerieID_seq")
private Integer categorieTresorerieId;
@MapsId
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "regroupementCategorieTresorerieCode", insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private RegroupementCategorieTresorerie regroupement;
@MapsId
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "typeCategorieTresorerieId", insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private TypeCategorieTresorerie typeCategorieTresorerie;
private Boolean estActif = true;
Join class RegroupementCategorieTresorerie, is the one which hibernate try to insert automatically:
@Entity
public class RegroupementCategorieTresorerie implements EstTraductible
{
@Id
private String regroupementCategorieTresorerieCode;
@Transient
private String libelle;
Join column TypeCategorieTresorerie:
@Entity
public class TypeCategorieTresorerie
{
@Id
@Column(name = "TypeCategorieTresorerieID")
private Integer typeCategorieTresorerieId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "CategorieTresorerieFamilleID", insertable = false, updatable = false)
@Fetch(FetchMode.JOIN)
private CategorieTresorerieFamille famille;
How can i do to insert CategorieTresorerie without automatically insert joined columns ?
Thank you for your help.
Not sure what exactly you're trying to do without seeing the insert code, but if you're trying to join an already existing entity try fetching it and point to the fetched entity (with the id) so hibernate will not try to create a new record.