【问题标题】:If is it possible save the same object to two different table Room?如果可以将同一个对象保存到两个不同的表房间吗?
【发布时间】:2019-01-08 00:28:28
【问题描述】:

假设我有 obj

class Human{
List<Car> demagedCars = new ArrayList();
List<Car> newCars = new ArrayList();
}

@Entity(tableName = "carstable")
class Car{
@PrimaryKey(autogenerate = true)
public long id;

public String name;
}

因此,房间将创建一个名为 carstable 的表,我将从两个列表 newCarsdemagedCars 中保存到该表中的对象

我怎样才能让它回到两个列表?

我认为可以创建两个表demagedcarstablenewcarstable

并根据列表保存demagedCarsdemagedcarstablenewCarsnewcarstable

但据我了解,没有办法为表格创建空间?

我不想改变我的对象...

我只需要在一个数据库中创建 2 个表。当我得到demagedCars 的列表时,我只需要告诉他I want you to save to demagedcarstable,当我得到newCars 列表时,我告诉他I want you to save to newcarstable。因此,我有 1 个数据库和 2 个表,我要在哪里保存这个 Car 对象取决于我。问题是:有可能吗?

欢迎提问

【问题讨论】:

    标签: java android android-room


    【解决方案1】:

    从结构的角度来看,我相信你应该将它们全部存储在同一个表中,比如 carsTable,并在汽车对象中添加一个状态字段。然后,您可以查询您的 carsTable 并使用该状态字段过滤 WHERE 子句中的结果。

    编辑:

    从 RoomDB 的角度来看,您的查询看起来像这样:

    @Query("SELECT * FROM carsTable WHERE status = :status")
    public Car[] findCarsByStatus(String status);
    

    【讨论】:

    • 我不确定是否有一个好的示例...我所需要的只是在一个数据库中创建 2 个表。当我得到demagedCars 的列表时,我只需要告诉他I want you to save to demagedcarstable,当我得到newCars 列表时,我会告诉他I want you to save to newcarstable。因此,我有 1 个数据库和 2 个表,我想在哪里保存这个 Car 对象取决于我。问题是:有可能吗?
    【解决方案2】:

    你可以为同一个对象拥有尽可能多的房间数据库

    @Database(entities = Car.class, version = 1, exportSchema = false)
    public abstract class newCarsDatabase extends RoomDatabase {
    private static final String LOG_TAG = newCarsDatabase.class.getSimpleName();
    private static final Object LOCK = new Object();
    private static newCarsDatabase sInstance;
    
    public static newCarsDatabase getInstance(Context context) {
        if (sInstance == null) {
            synchronized (LOCK) {
                Log.e(LOG_TAG, "creating new database");
                String databaseName = "new_cars";
                sInstance = Room.databaseBuilder(context.getApplicationContext(), newCarsDatabase.class, databaseName).build();
            }
        }
        Log.e(LOG_TAG, "Getting the database instance");
        return sInstance;
    }
    
    public abstract CarsDao favDao();
    }  
    

    对于损坏的

     @Database(entities = Car.class, version = 1, exportSchema = false)
    public abstract class demagedCarsDatabase extends RoomDatabase {
    private static final String LOG_TAG = demagedCarsDatabase.class.getSimpleName();
    private static final Object LOCK = new Object();
    private static demagedCarsDatabase sInstance;
    
    public static demagedCarsDatabase getInstance(Context context) {
        if (sInstance == null) {
            synchronized (LOCK) {
                Log.e(LOG_TAG, "creating new database");
                String databaseName = "damaged_cars";
                sInstance = Room.databaseBuilder(context.getApplicationContext(), demagedCarsDatabase.class, databaseName).build();
            }
        }
        Log.e(LOG_TAG, "Getting the database instance");
        return sInstance;
    
    }
    
    public abstract CarsDao DamagedDao();
    }
    

    此外,您可以简单地向每个班级添加数据,您可以对他们两个都使用相同的 Dao,或者如果您愿意,您可以为每个班级制作一个 Dao,但您只有一个课程,我建议您制作一个两人的道

    【讨论】:

    • 我不确定是否有一个好的示例...我所需要的只是在一个数据库中创建 2 个表。当我得到demagedCars 的列表时,我只需要告诉他I want you to save to demagedcarstable,当我得到newCars 列表时,我会告诉他I want you to save to newcarstable。因此,我有 1 个数据库和 2 个表,我要在哪里保存这个 Car 对象取决于我。问题是:有可能吗?
    猜你喜欢
    • 2015-01-16
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2019-07-10
    • 2012-01-16
    • 2022-06-16
    • 1970-01-01
    • 2019-11-03
    相关资源
    最近更新 更多