[英]JpaRepository save not returning the attributes of nested objects updated by id
我有一個正在更新的用戶提出了一個補丁請求,並且允許客戶端發送一個帶有圖像 ID 的 json 請求,這樣我就可以分配給用戶
{"image": {"id": 3}}
而完整的圖像 object 是這樣的
"image": {
"id": 3,
"fileUrl": "/uploads/identity.jpg",
"fileUrlType": "RELATIVE"
}
當我將 id 分配給新的 ImageEntity 並將其設置給我的用戶然后調用 repository.save(updatedUser) 時,它會正確保存到數據庫及其所有屬性。
問題是 save 方法的返回是返回一個用戶只有一個 id 屬性的圖像(其他屬性設置為 null)
PS:當我稍后嘗試從另一個請求中獲取用戶時,我發現它具有它需要的所有圖像屬性
有沒有辦法讓存儲庫?save(updatedUser) 返回用戶。 圖像及其所有屬性?
我嘗試調用saveAndFlush而不是save但它不起作用,我也嘗試在保存后直接調用 getById(user.id) 並且它也不起作用
這是我的服務代碼
public User patchUser(User user, FullUserDto userDto) {
userMapper.updateUserFromDto(userDto, user);
return this.userRepository.save(user);
}
保存后調用 entityManager.refresh(user) 解決的問題
我發現我需要改變兩件事:
saveAndFlush(user)
而不是save(user)
entityManager.refresh(user)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.