如何使用ORMLite与抽象类? [英] How do you use ORMLite with an abstract class?

查看:111
本文介绍了如何使用ORMLite与抽象类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类外围。类传感器外围的延伸。我需要ORMlite实例我认为是previously保存周围物体。显然,任何试图实例外围沉思会导致 ClassInstantiationException 由于它的抽象性。怎样才可以有ORMlite加载任何外围对象,因为外围是抽象的?

下面是我在做什么样:

  @DatabaseTable(tableName值=外设)
抽象类外设{
    @DatabaseField(generatedId =真)
    INT _ID;
    @DatabaseField
    INT mSerial;
}

类传感器扩展外设{
}

一流高手扩展外设{
}
 

解决方案
  我

怎么能有ORMlite加载任何外设的对象,因为周围是抽象的?

我觉得从这个事实你的问题源于该 @DatabaseTable 必须是在传感器类。它不会伤害它也可以在基类,但它不会被检测到那里。

这将有一个名为外围用于两个超类一个表。任何领域(如 mSerial )从基类将被检测并用于在这两个的超类的字段。

唯一的限制是,ORMLite无法传感器区分。它现在缺少的功能,它可以让你得到那个都行的传感器并获得是。此外,它无法为他们的模式,如果超类有标记 @DatabaseField 各自的领域。

编辑:

要重申, ORMlite (作为二千○一十三分之三)不支持多个子类的这一点很重要在同一个表的。子类的将会的继承基类的数据库中的字段,但他们都各自在自己的表。这里有一个有关对这个话题的讨论邮件列表。

要做到这一点,你可以有3个表:1的基础类信息和2的子类。子类的表将有一个外键关联的基类行。

I have an base class Peripheral. Classes Sensor and Master are extensions of Peripheral. I need ORMlite to instantiate my Peripheral objects that were previously saved. Obviously any attempt to instantiate Peripheral reflectively will result in a ClassInstantiationException due to its abstractness. How can I have ORMlite load any Peripheral object since Peripheral is abstract?

Here is the sample of what I am doing:

@DatabaseTable(tableName="Peripheral")
abstract class Peripheral {
    @DatabaseField(generatedId="true")
    int _ID;
    @DatabaseField
    int mSerial;
}

class Sensor extends Peripheral {
}

class Master extends Peripheral {
}

解决方案

How can I have ORMlite load any Peripheral object since Peripheral is abstract?

I think your problem stemmed from the fact that the @DatabaseTable needs to be on the Sensor and Master classes. It won't hurt it to also be on the base class but it won't be detected there.

This will have one table named "Peripheral" used for both super-classes. Any fields (such as mSerial) from the base class will be detected and used as fields in both of the super-classes.

The only limitation is that ORMLite is unable to differentiate between Sensor and Master. It right now is missing the feature which allows you to get all rows that are a Sensor and get all rows that are a Master. Also, it cannot generate the schema for them if the super-classes have their own fields marked with @DatabaseField.

Edit:

It's important to reiterate that ORMlite (as of 3/2013) does not support multiple sub-classes in the same table. Subclasses will inherit the database fields from the base class but they have to each be in their own table. Here's a discussion about this topic on the mailing list.

To accomplish this, you could have 3 tables: 1 for the base class information and 2 for the sub-classes. The sub-class tables would have a foreign key to the associated base class row.

这篇关于如何使用ORMLite与抽象类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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