OneToMany 未加载到二级实体中
OneToMany is not loaded in second level entity
我正在使用 spring-data-mongodb 1.8.2 (spring-boot-starter 1.3.1) 我手头有一个相当简单的案例(在我在绝望中添加了 fetch eager):
@Document(collection = "class_room")
public class ClassRoom implements Serializable {
@Id
private String id;
@NotNull
@Field("name")
private String name;
@ManyToOne**(fetch = FetchType.EAGER)**
@JoinColumn(name = "school_id")
private School school;
[...]
}
@Document(collection = "school")
public class School implements Serializable {
@Id
private String id;
@NotNull
@Field("name")
private String name;
@OneToMany(mappedBy = "school"**, fetch = FetchType.EAGER**)
private Set<Article> articles = new HashSet<>();
[...]
}
存储库:
public 接口 SchoolRepository 扩展了 MongoRepository {
}
public interface ClassRoomRepository extends MongoRepository<ClassRoom,String> {
}
以及资源:
@RequestMapping(value = "/schools",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<School> getAllSchools() {
return schoolRepository.findAll();
}
@RequestMapping(value = "/classRooms",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<ClassRoom> getAllClassRooms() {
return classRoomRepository.findAll();
}
现在,有人可以向我解释为什么当我执行 'schoolRepository.findAll()' 时文章会正确加载吗?
但不是当我执行 'classRoomRepository.findAll()'?
我该如何实现?
长话短说;博士
一个学校有一套文章
一个教室有一个学校。
当我直接访问一个学校时:我看到了文章的集合
通过classRoom访问学校时,Article集合为空
您使用对象关联的方法有点不对。在具有 Mongo 的 Spring 数据中,定义注释以描述关联如何发生的概念不是标准方法。
如果您在此处查看文档http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mapping-chapter.html,它有助于提供更清晰的信息。
但需要强调的是,Mongo 使用了嵌入式对象的概念,因此理想情况下,您的数据结构可以类似于:
@Document(collection = "class_room")
public class ClassRoom implements Serializable {
@Id
private String id;
private String name;
private School school;
// Where School has the following fields and structure:
// private String id;
// private String name;
// private Set<Article> articles = new HashSet<>()
}
如果您希望将学校嵌入到 ClassRoom 中,您可以像上面那样保留它,否则您可以将学校作为一个单独的集合。所以:
@Document(collection = "school")
public class School implements Serializable {
@Id
private String id;
private String name;
private Set<Article> articles = new HashSet<>();
[...]
}
在上面的School中,它是自己的一个集合,并没有嵌入到ClassRoom中。
通常,在处理 Mongo 或 NoSQL/Graph 数据库时,您必须以不同于传统 ORM 方法的方式思考。
我正在使用 spring-data-mongodb 1.8.2 (spring-boot-starter 1.3.1) 我手头有一个相当简单的案例(在我在绝望中添加了 fetch eager):
@Document(collection = "class_room")
public class ClassRoom implements Serializable {
@Id
private String id;
@NotNull
@Field("name")
private String name;
@ManyToOne**(fetch = FetchType.EAGER)**
@JoinColumn(name = "school_id")
private School school;
[...]
}
@Document(collection = "school")
public class School implements Serializable {
@Id
private String id;
@NotNull
@Field("name")
private String name;
@OneToMany(mappedBy = "school"**, fetch = FetchType.EAGER**)
private Set<Article> articles = new HashSet<>();
[...]
}
存储库: public 接口 SchoolRepository 扩展了 MongoRepository {
}
public interface ClassRoomRepository extends MongoRepository<ClassRoom,String> {
}
以及资源:
@RequestMapping(value = "/schools",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<School> getAllSchools() {
return schoolRepository.findAll();
}
@RequestMapping(value = "/classRooms",
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public List<ClassRoom> getAllClassRooms() {
return classRoomRepository.findAll();
}
现在,有人可以向我解释为什么当我执行 'schoolRepository.findAll()' 时文章会正确加载吗? 但不是当我执行 'classRoomRepository.findAll()'?
我该如何实现?
长话短说;博士 一个学校有一套文章 一个教室有一个学校。 当我直接访问一个学校时:我看到了文章的集合 通过classRoom访问学校时,Article集合为空
您使用对象关联的方法有点不对。在具有 Mongo 的 Spring 数据中,定义注释以描述关联如何发生的概念不是标准方法。
如果您在此处查看文档http://docs.spring.io/spring-data/data-mongo/docs/1.4.2.RELEASE/reference/html/mapping-chapter.html,它有助于提供更清晰的信息。
但需要强调的是,Mongo 使用了嵌入式对象的概念,因此理想情况下,您的数据结构可以类似于:
@Document(collection = "class_room")
public class ClassRoom implements Serializable {
@Id
private String id;
private String name;
private School school;
// Where School has the following fields and structure:
// private String id;
// private String name;
// private Set<Article> articles = new HashSet<>()
}
如果您希望将学校嵌入到 ClassRoom 中,您可以像上面那样保留它,否则您可以将学校作为一个单独的集合。所以:
@Document(collection = "school")
public class School implements Serializable {
@Id
private String id;
private String name;
private Set<Article> articles = new HashSet<>();
[...]
}
在上面的School中,它是自己的一个集合,并没有嵌入到ClassRoom中。
通常,在处理 Mongo 或 NoSQL/Graph 数据库时,您必须以不同于传统 ORM 方法的方式思考。