0
我有簡單的實體類:JPA休眠插入行事業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 [n/a];約束[C## NH.SYS_C0013920];嵌套的例外是org.hibernate.exception.ConstraintViolationException:無法執行語句
此錯誤即時得到只有一次....這取決於我手動之前有多少行插入....
是否有另方式如何處理這個問題,除了保存之前執行findlast?
謝謝您的建議。
您使用哪個數據庫? –
即時通訊使用Oracle 12c – Fleky
這裏的問題是你正在使用序列的主鍵,其起始值分配爲1 – shankarsh15