java - Spring Data Mongodb - repository for collection with different types -
i have mongo collection may contain 3 types of entities map java types:
- node
 - leaftype1
 - leaftype2
 
collection ment store tree-like structure using dbrefs of child nodes in parent entry.
i didn't find information subject in spring reference documentation i'm asking here: there way use repository mechanism work collection may contain different types of objects?
declaring several repositories different types in 1 collection seems not idea because struggle situations when queried object not of expected type , creating 1 repository abstract class possible types inherrit doesn't seems work.
to illustrate mean:
/**  * seems not safe  */ public interface noderepository extends mongorepository<node, string> { } public interface leaftype1repository extends mongorepository<leaftype1, string> { } public interface leaftype2repository extends mongorepository<leaftype2, string> { }  /**  * doesn't work @  */ public interface mycollectionrepository extends mongorepository<abstractmycollectionnode, string> { }      
if node\leaftype1\leaftype2 sub-classes of abstractmycollectionnode, things easy. declare repository write:
public interface mycollectionrepository extends mongorepository<abstractmycollectionnode, string> { }   we have done in project, , works good. spring data add property named '_class' documents in mongodb collection, can finger out class instantiate.
documents stored in 1 collection may have similarity, maybe can extract generic class them.
here code copied 1 of our projects:
entity:
public abstract class document {     private string id;      public string getid() {         return id;     }      public void setid(string id) {         this.id = id;     }     ....   public class webclipdocument extends document {     private string digest;     ...   repository:
public interface documentdao extends mongorepository<document, string>{ ...   and, if documents in mongodb collection not have "_class" property. can use converter:
when storing , querying objects convenient have mongoconverter instance handle mapping of java types dbobjects. however, may want `mongoconverter’s of work allow selectively handle conversion particular type or optimize performance.
Comments
Post a Comment