Spring数据MongoDB
我是Spring Data MongoDB的新手 我试图使用Spring数据MongoDB,mongodb,spring-data,Mongodb,Spring Data,我是Spring Data MongoDB的新手 我试图使用@DBRef引用另一个文档(新闻)中的文档(评论),使用spring-data-mongodb-1.7.0.RELEASE.jar 为了方便起见,我不会将任何内容作为rest调用的参数传递。数据库中存在注释集合。我得到以下异常: 严重:Servlet[appServlet]的Servlet.service()在上下文中 路径[/news]引发异常[处理程序处理失败;嵌套 异常为java.lang.NoSuchMethodError: D
@DBRef
引用另一个文档(新闻)中的文档(评论),使用spring-data-mongodb-1.7.0.RELEASE.jar
为了方便起见,我不会将任何内容作为rest调用的参数传递。数据库中存在注释集合。我得到以下异常:
严重:Servlet[appServlet]的Servlet.service()在上下文中
路径[/news]引发异常[处理程序处理失败;嵌套
异常为java.lang.NoSuchMethodError:
DBRef.(Ljava/lang/String;Ljava/lang/Object;)V]with
根本原因java.lang.NoSuchMethodError:
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");
}
}
import org.springframework.data.annotation.Id;
导入org.springframework.data.mongodb.core.mapping.Document;
@文件
公开课评论{
@身份证
私有字符串id;
私有字符串注释;
公共注释(字符串注释){
this.id=UUID.randomUUID().toString();
this.comment=注释
}
//接球手和接球手
}
导入org.springframework.data.annotation.Id;
导入org.springframework.data.mongodb.core.mapping.DBRef;
导入org.springframework.data.mongodb.core.mapping.Document;
@文件
公共类新闻{
@身份证
私有字符串id;
私有字符串标题;
私有字符串摘要;
@DBRef
私人名单评论;
公共新闻{
}
公共新闻(字符串标题、字符串摘要){
this.id=UUID.randomUUID().toString();
this.title=标题;
this.summary=摘要;
}
//接球手和接球手
}
@RestController
@请求映射(value=“/rest/test”)
公共类家庭控制器{
私有静态最终记录器Logger=LoggerFactory.getLogger(HomeController.class);
@RequestMapping(value=“/createnews”,method=RequestMethod.POST)
公共新闻{
info(“createNews方法已启动”);
新闻=新新闻(“新闻标题”、“新闻摘要”);
List commentList=新的ArrayList();
评论=新评论(“新闻评论”);
添加(注释);
news.setComments(commentList);
logger.info(“createNews方法结束”);
}
}
问题:
我无法将@DBRef
与spring-data-mongodb-1.7.0.RELEASE.jar一起使用
我正在征求专家们的意见。提前感谢。
只需删除“@DBRef”。
您不能使用DBRef将文档链接到列表。
SpringDataMongoDB将域类的列表作为文档中的子数组(作为嵌入文档)进行传输。
像这样:
{
_id: 100,
Title: "food",
summary: "xyz",
Comments: [ { id: "111", Comment: "shipping" }, { id: "111", Comment: "shipping" } ]
}
有关更多详细信息,请参阅。如何保存实体。首先必须保存引用的实体,然后保存父实体
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);
谢谢你的回答!请在回答中总结您的发现,以备将来读者参考,以防链接失效。谢谢您的回复。我想要的是存储为对象,而不是嵌入式文档。是的,嵌入式文档工作是的,子对象先于父对象保存。能否共享存储库配置以及如何保存实体?您是否看到您的评论被保存在mongo中,因为据我所知,您的交易在保存评论后和保存新闻时失败了?