如何为多态类定义 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/

10-10 00:58
查看更多