Spring Data Mongodb - repository for collection with different types(Spring Data Mongodb - 用于收集不同类型的存储库)
问题描述
我有一个 mongo 集合,它可能包含我映射到 java 类型的三种类型的实体:
I have a mongo collection that may contain three types of entities that I map to java types:
- 节点
- LeafType1
- LeafType2
使用父条目中子节点的 dbRefs 来存储树状结构的集合.
Collection is ment to store tree-like structure using dbRefs of child nodes in parent entry.
我在 Spring 参考文档中没有找到有关主题的任何信息,所以我在这里问:有没有办法使用 Repository 机制来处理可能包含不同类型对象的集合?
I didn't find any information about subject in Spring reference documentation so I'm asking here: Is there a way to use Repository mechanism to work with collection that may contain different types of objects?
在一个集合中为不同类型声明多个存储库似乎不是一个好主意,因为当查询的对象不是预期类型并为所有可能的继承类型似乎都不起作用的抽象类创建一个存储库时,我总是遇到这样的情况.
Declaring several repositories for different types in one collection seems like not very good idea because I always struggle with situations when queried object is not of expected type and creating one repository for abstract class that all possible types inherrit doesn't seems to work.
为了说明我的意思:
/**
* This seems not safe
*/
public interface NodeRepository extends MongoRepository<Node, String> { }
public interface LeafType1Repository extends MongoRepository<LeafType1, String> { }
public interface LeafType2Repository extends MongoRepository<LeafType2, String> { }
/**
* This doesn't work at all
*/
public interface MyCollectionRepository extends MongoRepository<AbstractMyCollectionNode, String> { }
推荐答案
如果 NodeLeafType1LeafType2 是 AbstractMyCollectionNode 的子类,那么事情就简单了.只需像你写的那样声明存储库:
If NodeLeafType1LeafType2 are sub-classes of AbstractMyCollectionNode, then things will be easy. Just declare the repository like you write:
public interface MyCollectionRepository extends MongoRepository<AbstractMyCollectionNode, String> { }
我们已经在一个项目中做到了这一点,而且效果很好.Spring Data 将在 mongodb 集合中的文档中添加一个名为 '_class' 的属性,以便它可以找出要实例化的类.
We have done this in a project, and it works good. Spring Data will add an property named '_class' to the documents in mongodb collection, so that it can finger out which class to instantiate.
存储在一个集合中的文档可能有一些相似之处,也许您可以为它们提取一个通用类.
Documents that stored in one collection may have some similarity, maybe you can extract a generic class for them.
以下是从我们的一个项目中复制的一些代码:
Here are some code copied from one of our projects:
实体:
public abstract class Document {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
....
<小时>
public class WebClipDocument extends Document {
private String digest;
...
存储库:
public interface DocumentDao extends MongoRepository<Document, String>{
...
并且,如果您在 mongodb 集合中的文档没有_class"属性.您可以使用 转换器:
And, if your documents in mongodb collection does not have the "_class" property. You can use Converter:
在存储和查询对象时,让 MongoConverter 实例处理所有 Java 类型到 DBObjects 的映射很方便.但是,有时您可能希望 MongoConverter 完成大部分工作,但允许您有选择地处理特定类型的转换或优化性能.
When storing and querying your objects it is convenient to have a MongoConverter instance handle the mapping of all Java types to DBObjects. However, sometimes you may want the `MongoConverter’s do most of the work but allow you to selectively handle the conversion for a particular type or to optimize performance.
这篇关于Spring Data Mongodb - 用于收集不同类型的存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Spring Data Mongodb - 用于收集不同类型的存储库
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
