I have a Location model class. I have an abstract class, Persister. I have an two implementations of the abstract class: MySQLPersister and MongoDBPersister. Reasoning is so that I can switch between using MySQL or MongoDB whenever I have to. Note that I am not using both at the same time. Just for more flexibility.
Problem is that the Location model class extends Model, MySQL will have to import play.db.jpa.Model. MongoDB will import play.modules.morphia.Model. This becomes a problem because every time I want to switch, not only do I have to change the Location model class, I also have to change the code in the implementations of the Persister due to the two libraries having different methods for the model.
For example, if I want to change from using JPA to Morphia, I have to comment out the code in MySQLPersister. The Model.find().fetch() returns Iterable in JPA, and the Model.find().filter().asList() in Morphia doesn't even exist when I import JPA.
How do I overcome this problem? I don't want to create a generic model class and two more identical model classes that import each database - that's too much duplication.
Basically I find the design of play-morphia and play-jpa unflexible:
JPA PlayMorphia
play.db.jpa.Model > play.modules.morphia.Model
javax.persistence.Entity > com.google.code.morphia.annotations.Entity
It would have been better if both play.db.jpa.Model
and play.modules.morphia.Model
were extended from a more generalized Model
class. This way I don't have to go about changing models if I wanted to implement both databases.
Thanks! Let me know if my explanation isn't clear.
I've decided on the following solution:
Create inner Location class in the MySQLPersister and MongoDBPersister and use those to do ORM and query the database. Then convert the results that are typed by the inner Location class to the general, front-end facing, LocationBean. Essentially, I duplicated the Location model twice inside inner classes within the two Persisters.
This way, if I ever want to switch between databases, I just need to change between initializing a new MySQLPersisteR() or a new MongoDBPersister. If I need to implement a new database, I just need to extend the LocationPersister again, with its own inner class for ORM.