Search code examples
javahibernateinheritancejpahibernate-annotations

Hibernate - Which inheritance strategy should I use for a class which isn't abstract, but sub-classes may not need tables


I have a simple inheritance relationship in my project, and I'd like to have the super class be abstract. Some of the inheriting classes will require extra database information, but others will not. I'm not sure which inheritance strategy to use.

I can't seem to find a straight answer as to weather a super-class can have an abstract class with the JOINED strategy.

I suspect the number of sub-classes won't become too large, and none of them should have much extra data, so perhaps a SINGLE_TABLE would suffice.

I really don't want to have extra tables for no reason, so TABLE_PER_CLASS is inappropriate.

I'd appreciate any guidance.

Thanks


Solution

  • If subclasses don't need tables, I would almost always just use a single table approach to inheritance. If, for some reason, one of the subclasses which does need a table has some absurd amount of extra fields (say 10+), the extra data might indicate that you need to create separate tables for some subclasses and flat tables for others.

    How this would be specifically mapped in hibernate, I'm not sure, but I think that's a sensible way to model it.