我使用Spring Data REST和JPA。我有一个用户实体,它与另一个名为AccountStatus的实体有一个多对一的关系,并在单独的RDBMS表中建模。JSON表示形式如下:
{
"id": "123"
"username": "user1",
"accountStatus": {
"id": "1",
"status": "Active"
}
}
用户实体中的关系为:
@ManyToOne(optional = false)
@JoinColumn(name = "account_state")
@Getter @Setter private AccountState accountState;
现在我尝试使用 /users/123 上的 PATCH 请求以及以下有效负载来更改帐户状态:
{"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替换为新状态,但没有成功。
我做错了什么?