Search code examples
javainheritanceactivejdbc

How to implement inherited models in ActiveJDBC with associations between superclass and another model?


I'd like to build something like this:

abstract class Owner extends Model {}
class User extends Owner {}
class Group extends Owner {}
class Thing extends Model {} 

where every Thing has one and only one Owner that can be a Group OR an User. How is it possible? Do I have to make Owner not abstract to let ActiveJDBC map the owner_id-column in the things-table to an owner? But how does it determine what kind of owner we have as http://javalite.io/inheritance says single table inheritance is not implemented in ActiceJDBC.


Solution

  • You need to use Polymorphic Associations. There is a difference between inheritance and ownership.

    ActiveJDBC inheritance is limited to passing common functionality to sub-classes. Then you need to use relationships to create "A has many B" and the like, and Polymorphic Associations seems like your best bet.

    So, in your case User and Group will be polymorphic parents of Thing. It is your choice then to create a separate class Owner (only if they share some functionality).

    So.. you will have three tables: USERS, GROUPS, THINGS.