I'm designing the architecture for a ODM in Java. I have a hierarchal structure with a top-level abstract Document
class. Implementations of DB objects will extend this class.
public abstract class Document {
ObjectId id;
String db;
String collection;
}
public class Student extends Document {
db = "School";
collection = "Student";
String name;
int age;
float gpa;
}
I want each class to be able to statically fetch results for its associated collection. For example, I'd like to do something like Students.get(Students.class, new ObjectId(12345))
which would return a Student
from the database. Note that I need to specify the class in the get()
method, this is a restriction of the object mapper library I'm using (Morphia).
What would be the best way to enforce each class to have this get()
method? There are a couple constraints:
get()
should be implemented as a static method- I'd like to avoid redundant code if possible and not have to specify the class for each implementation of
get()
. For example if I have aStudent
andTeacher
class, I want to avoid specifyingStudents.class
andTeacher.class
manually. I'm not sure this is possible though, sinceget()
is a static method.
I think this is a variation on the common abstract static Java problem, but I want to make sure I'm approaching it the right way first.