Reputation: 4493
Can the morphia single BasicDAO handle/query multiple collection, may by be by overloading function with the class parameter.
public class GenericDAO extends BasicDAO<T, K> {
/* override count impl*/
public long count(Class<T> clazz) {
return ds.getCount(clazz);
}
}
Is there any other way I can query two different collection using single DOA or it is better to make separate DAO for each collection.
Example For User and BlogEntry Collections
public class BlogEntryDAO extends BasicDAO<BlogEntry, ObjectId>
public class UserDAO extends BasicDAO<User, ObjectId>
Upvotes: 3
Views: 1923
Reputation: 58
The simple answer is NO,
The BasicDAO is made made on the assumption to deal with single collection/Entity as many of the DOA's function are Entity/Class and _id/primary key type based.
public class BasicDOA<T,K> implements DOA<T,K>
T should be a specific class
K should be a specific key (can be separate for different class) e.g ObjectId, String, Long etc
Example function
public Class<T> getEntityClass()
public T get(K id)
If you want to deal with multiple collection in single DAO then create your own generic DAO with custom methods and use DataStore to deal with different/specific collections.
public class MyDAO {
protected DatastoreImpl ds;
public count(Class<T> clazz) {
return ds.getCount(clazz);
}
public T get(Class<T> clazz, K id) {
return ds.get(clazz, id);
}
}
Upvotes: 2
Reputation: 30136
I think the intention of the DAO within Morphia is to have separate data access objects for each class/collection.
I would make two separate classes that each extend BasicDao.
I'm sure you could implement it as you suggest, by overloading the methods required, but the idea is to have one object through which you get the data for a given model.
Upvotes: 1