Home » Android » java – Create multiple tables for one class in ORMLite

java – Create multiple tables for one class in ORMLite

Posted by: admin June 15, 2020 Leave a comment


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{
    public string Name;

    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)


How to&Answers:

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.

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.

You could have:

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

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

The fields from Order will be in each of the fooorder and barorder tables.