如何使用 Spring Data(MongoDB) 映射通用集合?
我在映射我的内部收藏时遇到一些问题。 项目可以具有不同的内容类型。这是我的 Item 类:
@Document(collection = "items")
public class Item{
@Id
private ObjectId id;
private List<? super Content> content;
...
}
Content 是该 Item 的不同内容的基类。
public class YoutubeVideo implements Content{
private String url;
}
public class Image implements Content{
private String location;
}
...
保存(保存完成后没有问题)内容集合中包含一个图像和两个 YoutubeVideo 类的项目后,我得到这个 JSON
{ "_id" : { "$oid" : "4e423dcf7f3a0d12265da46c"}
"content" : [
{ "location" : "hdd path"} , { "url" : "url path"} , { "url" : "url path"}
]}
这不是我期望看到的 JSON。并且可以理解为什么无法加载和反序列化该文档。
java.lang.RuntimeException: Can not map ? super trngl.mongo.domain.content.Content
您将如何映射这种对象?我不想手动序列化和反序列化对象。是否可以?
找到兴趣转换器类: 映射显式转换器
I have some problems with mapping my inner collection.
Items can have different content types. Here is my Item class:
@Document(collection = "items")
public class Item{
@Id
private ObjectId id;
private List<? super Content> content;
...
}
Content is a base class for different content for this Item.
public class YoutubeVideo implements Content{
private String url;
}
public class Image implements Content{
private String location;
}
...
After saving (saving finishing with no problems) Item with one Image and two YoutubeVideo classes in content collection i getting this JSON
{ "_id" : { "$oid" : "4e423dcf7f3a0d12265da46c"}
"content" : [
{ "location" : "hdd path"} , { "url" : "url path"} , { "url" : "url path"}
]}
It is not this JSON I expected to see. And understandable why it is not possible to load and deserialize this document.
java.lang.RuntimeException: Can not map ? super trngl.mongo.domain.content.Content
How you would map this kind of object? I do not want to serialize and deserialize objects manualy. Is it possible?
Found interestion converters class: mapping-explicit-converters
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
List
就足够了,因为无论如何您都无法从混合内容列表访问具体类型。 (顺便说一句, super 在这里肯定是错误的,因为您不存储Content
的超类型,而是存储子类型。另一方面,扩展不会添加任何附加值)。List
应该适用于MongoTemplate
的最新快照,因为我们修复了自上一个里程碑版本以来的一些错误。如果您使用我们的存储库抽象,请确保Content
是包含 id 属性的抽象类。有一个开放的问题,您可能希望关注我们支持存储库管理类型的接口,例如出色地。It should be sufficient to use
List<Content>
as you can't access the concrete types from a mixed content list anyway. (Btw. super is definitely wrong here as your not storing supertyes ofContent
but subtypes. Extends on the other hand wouldn't add any additional value).List<Content>
should work with the latest snapshots forMongoTemplate
as we fixed quite some bugs since the last milestone release. If you're using our repository abstraction make sureContent
is an abstract class containing the id property. There is an open issue you might wanna watch for us to support interfaces as repository managed types as well.仅供参考(因为我们在试图找出为什么我们的对象作为成员的
LinkedHashMaps
返回后寻找这个早上好),这个问题也发生了(在 1.0.2 中)。 RELEASE),当您尝试以以下形式存储Collection
时:如上所述,解决方案是将其切换为
列表<内容>
。Just as an FYI (since we hunted for this for a good morning after trying to figure out why we were getting our objects returned as
LinkedHashMaps
of the members), this issue also occurs (in 1.0.2.RELEASE), when you try to store aCollection<Content>
in the form:The solution, as above, is to switch it to a
List<Content>
.代码
列表 content;
创建包含元素和类型的列表,该列表扩展内容类。正如我所见,内容 - 是一个接口,因此您不能为其指定通用绑定。我建议您创建抽象类 Content 并通过您的类 YoutubeVideo 和 Image 扩展该类。之后,代码
List content;
会正常工作。谢尔盖.
Code
List<? super Content> content;
creates list with elements, with type, which extends class Content. As I see, Content - is an interface, so you cannot specify generic binding for it.I'd suggest you to create abstract class Content and extend this class by your classes YoutubeVideo and Image. After that, code
List<? super Content> content;
will work fine.Sergey.