spring-data-mongodb 对象关联 @DBRef学习 之疑问
2014-04-04 09:50
447 查看
在spring-data-mongodb中,可以使用注解@DBRef来设置对象的关联,@DBRef在包org.springframework.data.mongodb.core.mapping.DBRef;中
例如,如下两个关系实体:
新建一个Student对象,将两门课程赋值给Student对象(注:这两门课程在数据库中并没有),然后保存到数据库。
按逻辑来讲,如果数据库中并不存在这两门课程,由于做了关联,在保存Student对象时,应该报错才对,可是为什么能够保存成功。保存的数据如下:
如此可看出,@DBRef已关联成功。可是在course这个collection中并没有c02,c01这两门课程,这样做的意义何在?(集合course此时还是空的,并没任何数据)
对于做了关联的对象之间的保存,删除等操作,spring-data-mongodb并不作检查就直接保存,删除了,这一块我有点不明白,意义何在。这样和内嵌文档的实质区别在哪里?
关于查询:
如果做了关联,查询的时候会是什么样子的呢?
下面我将集合collection中插入三条数据:
然后进行查询Student操作:
查询出的结果是:
Student [studentId=2009011, studentName=lklk,age=21, courses=[ Course [courseId=c02, courseName=english], Course[courseId=c01, courseName=math]]]
也就是说,此时查询的时候,spring-data-mongodb会将关联的Course实体也查询了出来,这一点是理想中的结果。
针对我提出的第一点,使用了@DBRef注解做了对象关联后,在插入,删除操作时,并不检查对象之间的关联关系就直接操作了,应该如何解决这个问题。请熟知mongodb的朋友讲解一下,小弟刚开始学mongodb,还请各位不吝赐教。
例如,如下两个关系实体:
Student(学生实体) @Document(collection="student") public class Student { @Id private String studentId; @Indexed(unique=true) private String studentName; private int age; @DBRef private List<Course> courses; //setter,getter方法略 } Course课程实体 @Document(collection="course") public class Course { @Id private String courseId; @Indexed(unique=true) private String courseName; //setter,getter }
新建一个Student对象,将两门课程赋值给Student对象(注:这两门课程在数据库中并没有),然后保存到数据库。
List<Course> courses = new ArrayList<Course>(); Course math = new Course(); Course english = new Course(); math.setCourseId("c01"); math.setCourseName("math"); english.setCourseId("c02"); english.setCourseName("english"); courses.add(english); courses.add(math); Student s = new Student(); s.setAge(21); s.setCourses(courses); s.setStudentId("2009011"); s.setStudentName("lklk"); studentService.addStudent(s);
按逻辑来讲,如果数据库中并不存在这两门课程,由于做了关联,在保存Student对象时,应该报错才对,可是为什么能够保存成功。保存的数据如下:
{ "_id" : "2009011", "_class" : "cn.com.coolcao.springmongo.entity.Student", "studentName" : "lklk", "age" : 21, "courses" : [ { "$ref" : "course", "$id" : "c02" }, { "$ref" : "course", "$id" : "c01" } ] }
如此可看出,@DBRef已关联成功。可是在course这个collection中并没有c02,c01这两门课程,这样做的意义何在?(集合course此时还是空的,并没任何数据)
对于做了关联的对象之间的保存,删除等操作,spring-data-mongodb并不作检查就直接保存,删除了,这一块我有点不明白,意义何在。这样和内嵌文档的实质区别在哪里?
关于查询:
如果做了关联,查询的时候会是什么样子的呢?
下面我将集合collection中插入三条数据:
/* 0 */ { "_id" : "c01", "_class" : "cn.com.coolcao.springmongo.entity.Course", "courseName" : "math" } /* 1 */ { "_id" : "c02", "_class" : "cn.com.coolcao.springmongo.entity.Course", "courseName" : "english" } /* 2 */ { "_id" : "c03", "_class" : "cn.com.coolcao.springmongo.entity.Course", "courseName" : "chinese" }
然后进行查询Student操作:
String id = "2009011"; Student s = studentService.queryById(id);
查询出的结果是:
Student [studentId=2009011, studentName=lklk,age=21, courses=[ Course [courseId=c02, courseName=english], Course[courseId=c01, courseName=math]]]
也就是说,此时查询的时候,spring-data-mongodb会将关联的Course实体也查询了出来,这一点是理想中的结果。
针对我提出的第一点,使用了@DBRef注解做了对象关联后,在插入,删除操作时,并不检查对象之间的关联关系就直接操作了,应该如何解决这个问题。请熟知mongodb的朋友讲解一下,小弟刚开始学mongodb,还请各位不吝赐教。
相关文章推荐
- Spring Data MongoDB]学习笔记--牛逼的MongoTemplate
- [Spring Data MongoDB]学习笔记--MapReduce
- Spring data mongoDB学习(2)
- MongoDB的使用学习之(七)MongoDB的聚合查询(spring-data-mongodb两种方式)附项目源码
- [Spring Data MongoDB]学习笔记--注册一个Mongo实例
- [Spring Data MongoDB]学习笔记--MongoTemplate查询操作
- [Spring Data MongoDB]学习笔记--牛逼的MongoTemplate
- spring-data-mongodb保存对象时去掉_class列
- spring data mongodb学习以及为repository提供可扩展的自定义方法
- [Spring Data MongoDB]学习笔记--MongoTemplate插入修改操作
- Spring Data Jpa 配合MongoDB实现持久层对象属性动态增加
- Spring data mongoDB学习(1)
- Spring-Data-MongoDB保存对象的时候,如何不保存_class字段
- [Spring Data MongoDB]学习笔记--_id和类型映射
- [Spring Data MongoDB]学习笔记--建立数据库的连接
- Spring Data Rest学习篇----Object Representation (实体对象展现)
- spring data mongodb学习以及为repository提供可扩展的自定义方法
- Spring Data MongoDB入门-访问mongodb
- SpringBoot学习6之Logback日志导入mongodb
- spring-data-solr官方学习文档介绍