我想在数据库中插入实体" CategorieTresorerie"有两个连接列。问题是,其中一个(RegroupementCategorieTresorerie)试图自动插入...但是这个已经在数据库中,所以我有一个重复的键值违例异常。
错误代码:
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
我要插入的课程:
@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;
加入类RegroupementCategorieTresorerie,是hibernate尝试自动插入的那个:
@Entity
public class RegroupementCategorieTresorerie implements EstTraductible
{
@Id
private String regroupementCategorieTresorerieCode;
@Transient
private String libelle;
加入专栏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;
如何在不自动插入连接列的情况下插入CategorieTresorerie?
感谢您的帮助。
答案 0 :(得分:0)
不确定在没有看到插入代码的情况下你究竟想要做什么,但是如果你正在尝试加入已经存在的实体,请尝试获取它并指向获取的实体(带有id),这样hibernate就不会尝试创建新记录。