Java 使用REST延迟加载Spring数据DBRef-500服务器错误

Java 使用REST延迟加载Spring数据DBRef-500服务器错误,java,spring,mongodb,spring-data,spring-data-mongodb,Java,Spring,Mongodb,Spring Data,Spring Data Mongodb,我在MongoDB中使用Spring数据 我的目标如下: @Document(collection = "Notification") public class Notification { @Id private String id; @DBRef (lazy=true) private User sender; } 当我尝试使用以下方法返回此对象时,我在浏览器控制台中收到一个500错误,我看不到任何数据 @RequestMapping(value = "/c

我在MongoDB中使用Spring数据

我的目标如下:

@Document(collection = "Notification")
public class Notification {
    @Id
    private String id;
    @DBRef (lazy=true)
    private User sender;
}
当我尝试使用以下方法返回此对象时,我在浏览器控制台中收到一个500错误,我看不到任何数据

@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,以便在加载时加载与您的实体用户关联的所有实体通知。这种情况会发生,因为它通过使用select查询来获取数据,将您请求的用户带入,然后通过另一个查询获取与其关联的通知,当lazy为true时,该查询将被跳过,因此您必须将lazy设置为false,或将fetch mode设置为join,以便它将关联的实体带入


这可能会对你有更多帮助。

我认为这正是你的问题,所以不幸的是,还没有其他解决方案。Uggh yuck,这是非常不幸的。我想我现在会保持这种渴望。谢谢你的评论,如果你将其作为答案发布,我将在明天醒来时批准你的答案。