我有简单的实体类:
@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,还有另一种方法可以解决这个问题吗?
感谢您的建议。
答案 0 :(得分:0)
我解决了我的问题。我在数据库中按顺序插入了数字 为此目的使用序列是必要的。