Stackee007
Stackee007

Reputation: 3244

Spring Mongo Repository Polymorphism

How do I define Repository interface for polymorphic classes

Ex.

abstract class Source { public String name }

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

Now I know I cannot define a repository interface like

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

or

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

Is defining simple plain interface and have an implmentation class is the only way?

Upvotes: 4

Views: 2032

Answers (1)

Stackee007
Stackee007

Reputation: 3244

Well letting spring to associate mongo document to java class mapping through '_class' attribute would work fine.

Mongo document would like some like this

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

Upvotes: 1

Related Questions