[英]DB2 Auto increment primary key column not working with JPA entity in Spring Boot
[英]Auto Increment filed not returning value (Not Primary Key) - Spring boot JPA
根據我的要求,我有自動增量字段而不是鍵 。 在調用存儲庫save()之后,我需要獲取該自動增量字段的值。
merchantEntity = merchantRepository.save(merchantEntity);
merchantEntity.getMerchId() // returns null
但有些人如果沒有獲得自動遞增的值,則返回null。 這是我的實體類。
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id")
private Integer merchId;
我嘗試過使用其他幾種解決方法,例如在保存后刷新實體。 但結果是一樣的。
merchantEntity = merchantRepository.saveAndFlush(entity); // still returns null
merchantEntity = merchantRepository.save(entity);
merchantRepository.flush();
MerchantEntity toBeUpdatedMerchantEntity =
merchantRepository.findOne(merchantEntity.getId()); // still returns null
是否有可能實現我所期待的。 任何幫助都非常感謝。 謝謝
獲得此自動遞增值的唯一方法是刷新記錄。
因為JPA只提取主鍵,因為在EntityManager.persist()
之后必須設置ID。
所以你必須用findOne()
或EntityManager.refresh()
重新加載記錄
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.