在ORMLite一个类中创建多个表 [英] Create multiple tables for one class in ORMLite

查看:396
本文介绍了在ORMLite一个类中创建多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Android上ORMLite,并有以下问题: 是否有可能基于单一的Java类来创建多个表?

I'm using ORMLite on Android and have the following question: Is it possible to create many tables based on a single Java class?

表不应区别仅在于他们的名字,以及对它们的访问应该是名字。

The tables should only differ in their names, and the access to them should be by name.

例如,如果我有一个类:

For example if I have a class:

public class Order{
    @DatabaseField
    public string Name;

    @DatabaseField
    public string Amount;
}

和我有模块,可以创建订单的一个动态的数字。我想每个模块有它自己的表,但所有表都应该有一个类似的模式。

And I have a dynamic number of modules that can create orders. I want every module to have its own table, but all tables should have a similar schema.

我知道我可以在订单类,它指示源模块的名称添加一个字段,并拥有所有的订单居住在一个表中,但我不知道是否有一种方法来区分表(更快的查询,更快的缺失从同一模块等的订单)的

I know I can add a field in the Order class that indicates the source module name, and have all the orders live in one table, but I was wondering if there's a way to separate the tables (for faster queries, faster deletion of orders from the same module and so on)

谢谢!

推荐答案

最大的问题在这里是你所说的动态的意思。如果你的意思是,你需要创建这些模块订单 S上的苍蝇,你不知道他们的名字事先那么我相信一​​个模块名称字段是一个有效的方法做这个事情。为每种类型的顺序来创建一个表看起来像premature优化我 - 复杂性没有充分理由

The big question here is what do you mean by "dynamic". If you mean that you need to create these module Orders on the fly and you don't know their names beforehand then I believe a module-name field is the an efficient way to do this. To create a table for each type of order seems like premature optimization to me -- complexity without good reason.

也就是说,你可以通过实例化一个<一个实现动态类href="http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/DatabaseTableConfig.html"><$c$c>DatabaseTableConfig每个表和定义的类编程的。

That said, you can accomplish dynamic classes by instantiating a DatabaseTableConfig for each table and defining the class programmatically.

如果你真的不意味着动态的,那么要完成把每个模块订单在其自己的表将与子类最简单的方法。

If you don't really mean dynamic, then the easiest way to accomplish putting each module Order in its own table would be with subclasses.

您可以有:

@DatabaseTable
public class FooOrder extends Order {
    // fields will be gotten from Order
}

@DatabaseTable
public class BarOrder extends Order {
    // fields will be gotten from Order
}

订单的字段将在每个 fooorder barorder 表。

这篇关于在ORMLite一个类中创建多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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