Created
June 7, 2012 12:09
-
-
Save spinscale/2888456 to your computer and use it in GitHub Desktop.
Using a morphia entity in several db collections in MongoDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
I have the need to use one morphia entity in several collections, because I use mongodb as a queue | |
The only solution I have found so far is to create a custom datastore, which maps to a certain db collection. This was my try: In case there is a cleaner solution, feel free to drop me a note | |
Datastore importDatastore = new CustomDatastore(Product.class, mongo, morphia, "import"); | |
Datastore exportDatastore = new CustomDatastore(Product.class, mongo, morphia, "export"); | |
ProductDao importQueueDao = new ProductDao(Product.class, importDatastore); | |
ProductDao exportQueueDao = new ProductDao(Product.class, exportDatastore); | |
*/ | |
public class CustomDatastore extends DatastoreImpl { | |
private Class<Product> clazz; | |
private String dbCollection; | |
public CustomDatastore(Class<Product> clazz, Mongo mongo, Morphia morphia, String dbCollection) { | |
super(morphia, mongo, "ludr"); | |
this.clazz = clazz; | |
this.dbCollection = dbCollection; | |
} | |
@SuppressWarnings("rawtypes") | |
@Override | |
public DBCollection getCollection(Class clazz) { | |
String collName = this.clazz.equals(clazz) ? dbCollection : mapr.getCollectionName(clazz); | |
DBCollection dbC = getDB().getCollection(collName); | |
return dbC; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment