ormlite是否支持继承? [英] Does ormlite support inheritance?

查看:102
本文介绍了ormlite是否支持继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ormlite是否支持这三种继承策略?

Does ormlite support this three inheritance-strategies?


  • 每个类层次结构的表

  • 每个子类的表

  • 每个具体类别的表格

我发现关于ORMLite用户组的讨论,但我不确定它是否回答了我的问题。

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

特别我想使用每个类层次结构的表。如果他们不是ormlite框架的支持,我将创建一个类并忘记继承,但我更喜欢使用继承。

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.

推荐答案

ORMLite 不会自动支持这些,不。我不确定你是否真的阅读了你所链接的主题,因为它非常具体:

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:



什么我需要的是:如果我将A类作为超类,并且我有直接的子类B和C,并且我对A类的所有记录/对象运行查询,我想要拥有A,B和C类的所有对象返回。

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.

是的,ORMLite没有这样做,目前我没有计划支持它。除非我错误地知道如何实现这一点,否则这种复杂性超出了我所说的精简版。 Hibernate支持这种结构的方式是通过外键,多个表和魔术连接。

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.

这个问题给出了一些关于如何使用ORMLite来实现继承:

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


1)你可以拥有A类和一个相关的表 - 所以它不能是抽象的。然后你可以拥有B和C,它们是A的子类,并且所有的As字段都在一个单独的表中。然后你可以为A创建一个特殊的DAO,它也会查询B和C并将它们添加到A的结果中。

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)另一种方法是你可以使类B和C 成为子类,而是有一个外来对象到A.然后你可以查询所有的As,其中一些将是B的A部分和一些C的A部分而一些只是答:然后您还可以查询B和C并使用A DAO刷新并获取您无法投射的A字段。 A也可以有某种枚举,表示它是A,B还是C.你也可以做一些魔术,你可以说getSubClassObject(),如果要查看枚举并查询B或C dao为具有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.

这篇关于ormlite是否支持继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆