I’m trying to use MongoDB to store a series of documents. These document share some standard attributes while it has several variation. We implement the POJO with an inheritance. The base class is Document, while it has several sub-classes such as Invoice and Orders, which has several additional fields when compared with Document class.
class Document {
DocTypeEnum type;
String title;
}
class Invoice extends Document{
Date dueDate;
}
class Order extends Document{
List<LineItems> items;
}
Is there an ORM framework support query the collection and return a list of mixed objects (invoice, order, basic document, etc) according to its type field?
List<Document> results = DocCollection.find(...);
Thanks a lot!
Morhia supports polymorphism even without requiring a type enum or anything. It stores the actual instance classname along with the rest of the data. Have a look at the @Polymorphic annotation.