我使用Spring Data REST和JPA。我有一個用戶實體與另一個叫做AccountStatus的多對一關係建模在一個單獨的RDBMS表中。 JSON表示如下:如何更新與Spring Data REST的@ManyToOne關係?
{
"id": "123"
"username": "user1",
"accountStatus": {
"id": "1",
"status": "Active"
}
}
在用戶實體的關係是:
@ManyToOne(optional = false)
@JoinColumn(name = "account_state")
@Getter @Setter private AccountState accountState;
現在我嘗試使用上/用戶/ 123和有效載荷補丁請求更改賬戶狀態:
{"accountState":{"id":0}}
但我得到一個錯誤:
"identifier of an instance of com.domain.account.AccountState was
altered from 1 to 0; nested exception is org.hibernate.HibernateException:
identifier of an instance of com.domain.account.AccountState was
altered from 1 to 0"
我也嘗試使用@ HandleBeforeSave/@ HandleBeforeLinkSave從存儲庫中提取新的AccountState,並取代user.accountStatus而沒有成功。
我在做什麼錯?
的[休眠可能重複:如何解決「實例標識符從X更改爲Y」?](http://stackoverflow.com/questions/4179166/hibernate-how-to-fix-identifier-of-an-instance-altered-from-x-to- y) – Makoto
@Makoto,我想另外一個問題是關於改變一個擁有實體的PK,我的問題是改變一個沒有在用戶和AccountState之間定義級聯的子關係。 – florind