【问题标题】:Spring Data MongoDB春季数据 MongoDB
【发布时间】:2015-05-26 21:59:52
【问题描述】:

我是 Spring Data MongoDB 的新手。

我正在尝试使用@DBRef 引用另一个Document(新闻)的Document(评论)与spring-data-mongodb-1.7.0.RELEASE.jar .

为了简单起见,我没有将任何东西作为参数传递给 rest 调用。 Comments 集合存在于数据库中。我收到以下异常:

严重:Servlet.service() for servlet [appServlet] 在上下文中 路径 [/news] 抛出异常 [处理程序处理失败;嵌套的 例外是 java.lang.NoSuchMethodError: com.mongodb.DBRef.(Ljava/lang/String;Ljava/lang/Object;)V] 与 根本原因 java.lang.NoSuchMethodError: com.mongodb.DBRef.(Ljava/lang/String;Ljava/lang/Object;)V

以下是我的代码:

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Comments{
    @Id
    private String id;

private String Comment;

public Comments(String comment){
    this.id = UUID.randomUUID().toString();
    this.comment = comment
}
     //getters & setters
}


import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class News{

@Id
private String id;
private String Title;
private String summary;
@DBRef
private List<Comments> comments;

public News(){
}

public News(String title, String summary){
    this.id = UUID.randomUUID().toString();
    this.title = title;
    this.summary = summary;

}

    //getters and setters
}



@RestController
@RequestMapping(value = "/rest/test")
public class HomeController {

private static final Logger logger =                   LoggerFactory.getLogger(HomeController.class);

@RequestMapping(value = "/createnews", method = RequestMethod.POST)
public void createNews() {
    logger.info("createNews method started");
    News news = new News("News Title", "News summary");
    List<Comments> commentList = new ArrayList<Comments>();
    Comments comments = new Comments("News Comments");
    commentList.add( comments );
    news.setComments( commentList );    

    logger.info("createNews method ended");

    }
}

问题:

我无法将@DBRef 与 spring-data-mongodb-1.7.0.RELEASE.jar 一起使用。

我正在寻求专家的建议。提前致谢。

【问题讨论】:

  • 感谢绫野的回复。我想要的是存储为对象而不是嵌入式文档。是的,嵌入式文档有效

标签: mongodb spring-data


【解决方案1】:


只需删除“@DBRef”即可。
您不能使用 DBRef 将文档链接到列表。
Spring data mongodb 会将您的域类列表作为文档中的子数组传输(作为嵌入式文档)。 像这样:

{
   _id: 100,
   Title: "food",
   summary: "xyz",
   Comments: [ { id: "111", Comment: "shipping" },  { id: "111", Comment: "shipping" } ]
}

更多详情请见http://docs.mongodb.org/manual/tutorial/query-documents/#read-operations-embedded-documents。

【讨论】:

  • 感谢您的回答!请总结您的发现以回答未来的读者,以防链接失效。
【解决方案2】:

您如何保存您的实体。您首先必须保存被引用的实体,然后是父实体。

News news = new News("News Title", "News summary");
Comments comments = new Comments("News Comments");
news.setComments( Arrays.asList(comments) );    

mongoTemplate.save(comments);
mongoTemplate.save(news);

【讨论】:

  • 是的,子对象先保存在父对象之前。
  • 您能否分享您的存储库配置以及您如何保存实体?您是否看到您的 cmets 被保存在 mongo 中,因为据我所知,您的交易在保存 cmets 和保存新闻后失败?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 2014-12-21
  • 2019-10-15
  • 1970-01-01
相关资源
最近更新 更多