JPA hibernate插入行导致ConstraintViolationException(JPA hibernate insert row cause ConstraintViolationExc

编程入门 行业动态 更新时间:2024-10-28 19:25:07
JPA hibernate插入行导致ConstraintViolationException(JPA hibernate insert row cause ConstraintViolationException)

我有简单的实体类:

@Entity @Table(name = "NH_ENTITIES") @Getter @Setter @ToString public class Entita { /** * Primary key. */ @Id @Column(name = "PK", nullable = false, length = NHConstants.DB_STANDARD_LENGTH) @GeneratedValue(strategy = GenerationType.AUTO, generator = "nh_entities_gen") @SequenceGenerator(name = "nh_entities_gen", sequenceName = "NH_ENTITIES_SEQ", initialValue = 1, allocationSize = 1) private int pk; /** * Entity name. */ @NotEmpty(message = "Entita je povinná") @Size(max = NHConstants.DB_MAX_LENGTH, message = "Maximální počet znaků: " + NHConstants.DB_MAX_LENGTH) @Column(name = "ENTITY", unique = true, nullable = false, length = NHConstants.DB_MAX_LENGTH) private String entity = ""; /** * For Hibernate only. */ public Entita() { } }

和我的服务方法:

@Override @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void saveEntity(Entita entity) { log.debug("Start transaction saveEntity"); this.entityRepo.save(entity); log.debug("Transaction saveEntity finished"); }

当我通过某个sql客户端手动向这个实体插入行/行时,以编程方式执行saveEntity即时获取错误无法执行语句; SQL [不适用]; 约束[C ## NH.SYS_C0013920]; 嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句

这个错误我只得到一次....这取决于我手动插入多少行....

除了在保存之前执行findlast之外,还有另一种方法可以解决这个问题吗?

谢谢你的建议。

i have simple entity class:

@Entity @Table(name = "NH_ENTITIES") @Getter @Setter @ToString public class Entita { /** * Primary key. */ @Id @Column(name = "PK", nullable = false, length = NHConstants.DB_STANDARD_LENGTH) @GeneratedValue(strategy = GenerationType.AUTO, generator = "nh_entities_gen") @SequenceGenerator(name = "nh_entities_gen", sequenceName = "NH_ENTITIES_SEQ", initialValue = 1, allocationSize = 1) private int pk; /** * Entity name. */ @NotEmpty(message = "Entita je povinná") @Size(max = NHConstants.DB_MAX_LENGTH, message = "Maximální počet znaků: " + NHConstants.DB_MAX_LENGTH) @Column(name = "ENTITY", unique = true, nullable = false, length = NHConstants.DB_MAX_LENGTH) private String entity = ""; /** * For Hibernate only. */ public Entita() { } }

and my service method:

@Override @Transactional(readOnly = false, propagation = Propagation.REQUIRED) public void saveEntity(Entita entity) { log.debug("Start transaction saveEntity"); this.entityRepo.save(entity); log.debug("Transaction saveEntity finished"); }

when i manually insert row/rows to this entity by some sql client and afterthat execute saveEntity programatically im getting the error could not execute statement; SQL [n/a]; constraint [C##NH.SYS_C0013920]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

This error im getting just once....it depends how many rows i inserted before manually....

Is there another way how to handle this issue except execute findlast before save?

Thank you for your suggestion.

最满意答案

我解决了我的问题。 我在DB中顺序插入了数字。为此目的使用序列是必要的。

I resolved my issue. i inserted exactly the numbers sequencially in DB It is neccessary to use the sequence for this purpose.

更多推荐

saveEntity,entity,Entita,NHConstants,execute,电脑培训,计算机培训,IT培训"/> <

本文发布于:2023-07-24 17:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1248821.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:hibernate   JPA   ConstraintViolationException   row   insert

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!