1

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.

Mischa
  • 13
  • 3

1 Answers1

0

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.

ipolevoy
  • 5,432
  • 2
  • 31
  • 46
  • Thanks for reply. The disadvantage of this solution is that a Thing can have a Group _and_ an User as Parents instead of having a unique Owner. Maybe it can be handled by validations - I haven't stepped in ActiveJDBC's validations yet. But the advantage is that it should work :-) I will give it a try. – Mischa Aug 09 '15 at 10:00
  • It seems very slowly I' getting it: Thig can NOT have both parents as the special colums prevent that. There's only one parent id an the type for it in the table. Great! – Mischa Aug 09 '15 at 14:01
  • It is working like a charm now. I created the class Owner because I do not always know what type it is exactly. Thing therefore has this method: public Owner getOwner() { if (this.getString("PARENT_TYPE").endsWith("Group")) { return this.parent(Group.class); } return this.parent(User.class); } – Mischa Aug 09 '15 at 15:48
  • Exactly right, this is why it is called a plymorphic relationship. – ipolevoy Aug 10 '15 at 02:59