簡體   English   中英

REST的Spring Data DBRef延遲加載-500服務器錯誤

[英]Spring data DBRef lazy loading with REST - 500 server error

我在MongoDB中使用Spring Data。

我有以下對象:

@Document(collection = "Notification")
public class Notification {
    @Id
    private String id;
    @DBRef (lazy=true)
    private User sender;
}

當我嘗試使用以下方法返回此對象時,在瀏覽器控制台中收到500 error ,並且看不到任何數據。

@RequestMapping(value = "/contactNotifications", method = RequestMethod.GET)
@ResponseBody
public List<Notification> getContactNotifications() {
    List<Notification> notifications = notificationService.findByUser(user.getId());
    return notifications;
}

但是,如果我刪除了lazy=true ,它似乎可以正常工作。

如何在使用lazy=true解決此問題?

我想, 也正是您的問題,所以很遺憾沒有(其他)解決方案, 不是急於負載。

您需要將lazy設置為false,以便在加載時加載與您的實體(用戶)關聯的所有實體(通知)。 之所以會發生這種情況,是因為它通過使用選擇查詢來獲取數據,方法是使用選擇查詢來帶入您請求的用戶,然后通過另一個查詢來獲取與它相關聯的通知,而當lazy為true時,該查詢將被跳過,因此您必須將lazy設置為false或設置提取模式“加入”,這樣它將帶來關聯的實體。

鏈接可能會為您提供更多幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM