繁体   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