Spring Mongo Repository 多态性

4

如何为多态类定义存储库接口

例如:

abstract class Source { public String name }

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

现在我知道我不能像这样定义存储库接口:
interface SourceRepo extends Repository<? extends Source, String>{....}

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

定义简单明了的接口并拥有一个实现类是唯一的方式吗?

1个回答

1

好吧,让Spring通过'_class'属性将Mongo文档与Java类映射关联起来就可以正常工作。

Mongo文档可能长这样

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接