开发者

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


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.


Too late to answer this question, but might help if others have a similar scenario.

Use a SingleTable strategy and if you thing one of the subclasses has more number of fields and need a separate table, then mark that subclass as a SecondaryTable using @SecondaryTable annotation and use PrimaryKeyJoin(there is an annotation for this) to join with superclass on pk column. Also, all the fields in the subclass should be mapped to secondaryTable explicitly using 'table' attribute in @column annotation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜