Does ormlite support inheritance?

2019-02-06 07:45发布

Does ormlite support this three inheritance-strategies?

  • table per class hierarchy
  • table per subclass
  • table per concrete class

I found this discussion on the ORMLite user group, but I'm not sure it answers my question.

Specially i want use "table per class hierarchy". If their isn't support from the ormlite framework i will make one class and forget the inheritance, but i would prefer to use inheritance.

1条回答
疯言疯语
2楼-- · 2019-02-06 08:10

ORMLite does not support these automatically, no. I'm not sure you actually read the thread you linked to because it's quite specific about it:

What I need is: If I have class A as a superclass, and I have direct subclasses B and C, and I run a query for all records/objects of class A, I do want to have all objects of class A, B and C returned.

Yeah, ORMLite does not do this and currently I have no plans to support it. The complexity of this, unless I am mistaken on how to accomplish this, is outside of what I'd say was "Lite". The way Hibernate supports such constructs, is by having foreign keys, multiple tables, and magic joins.

That question gives some ideas about how to use ORMLite to implement inheritance:

1) You could have class A and an associated table -- so it can't be abstract. Then you could have have B and C which subclass A and have all of As fields each in a separate table. You could then create a special DAO for A which would also query for B and C and add them into your results for A.

2) Another way to do it is you could make classes B and C not be subclasses but instead have a foreign object to A. You can then query all of the As and some of them would be B's A part and some C's A part and some just A. You then could also query Bs and Cs and use the A DAO to refresh and get the A's fields you would not be able to cast. A could also have some sort of enum which said whether it was a A, B, or C. You could also do some magic where you could say getSubClassObject(), if would look at the enum and query the B or C dao for the associated object with the right foreign key for the A object.

查看更多
登录 后发表回答