gpt4 book ai didi

Spring Mongo Repository 多态性

转载 作者:可可西里 更新时间:2023-11-01 09:15:47 25 4
gpt4 key购买 nike

如何为多态类定义 Repository 接口(interface)

例。

abstract class Source { public String name }

class InternalSource extends Source { public int internalId }
class ExternalSource extends Source { public String contact }

现在我知道我不能像这样定义存储库接口(interface)

interface SourceRepo extends Repository<? extends Source, String>{....}

interface SourceRepo extends Repository<Source, String> { ....}

定义简单的普通接口(interface)并拥有一个实现类是唯一的方法吗?

最佳答案

好吧,让 spring 通过“_class”属性将 mongo 文档关联到 java 类映射就可以了。

Mongo 文档会喜欢这样的

{_id : "xxx", name : "abc", internalId : 123, _class = "...InternalSource" }
{_id : "xxx", name : "abc", contact: "John doe", _class = "...ExternalSource"}

关于Spring Mongo Repository 多态性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12189855/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com