如何使用 Spring Data(MongoDB) 映射通用集合?

发布于 2024-11-28 13:47:36 字数 1020 浏览 0 评论 0原文

我在映射我的内部收藏时遇到一些问题。 项目可以具有不同的内容类型。这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时常饿 2024-12-05 13:47:36

使用 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 of Content but subtypes. Extends on the other hand wouldn't add any additional value).

List<Content> should work with the latest snapshots for MongoTemplate as we fixed quite some bugs since the last milestone release. If you're using our repository abstraction make sure Content 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.

倾城°AllureLove 2024-12-05 13:47:36

仅供参考(因为我们在试图找出为什么我们的对象作为成员的 LinkedHashMaps 返回后寻找这个早上好),这个问题也发生了(在 1.0.2 中)。 RELEASE),当您尝试以以下形式存储 Collection 时:

@Document(collection = "items")
public class Item{
@Id
private ObjectId id;   
private Collection<Content> content;
...
}

如上所述,解决方案是将其切换为列表<内容>

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 a Collection<Content> in the form:

@Document(collection = "items")
public class Item{
@Id
private ObjectId id;   
private Collection<Content> content;
...
}

The solution, as above, is to switch it to a List<Content>.

颜漓半夏 2024-12-05 13:47:36

代码列表 content; 创建包含元素和类型的列表,该列表扩展内容类。正如我所见,内容 - 是一个接口,因此您不能为其指定通用绑定。

我建议您创建抽象类 Content 并通过您的类 YoutubeVideoImage 扩展该类。之后,代码 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文