在会议室数据库中保存自定义数据类型列表时如何解决错误 [英] How to fix the error while saving list of custom data types in room database

查看:241
本文介绍了在会议室数据库中保存自定义数据类型列表时如何解决错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在房间数据库中保存自定义数据类型的列表,实际上,我希望其中一列应包含所有不必显示两个表的事务的列表.

I am trying to save a list of custom data types in room database, actually, I want one of the column should contain list of all transactions show that I don't have to two tables.

account.kt

@Entity(tableName = "account_table")
data class account(

    @ColumnInfo(name="name")
    val name:String,


    @ColumnInfo(name="transaction")

    val transactions:List<transaction>

){
    @PrimaryKey(autoGenerate = true)@ColumnInfo(name="account_id")
    val accountId:Int=0
}

transaction.kt

data class transaction(
    val amount:Float,
    val description:String,
    val date:String

)

现在,当我运行代码时,我得到一个错误

Now when I am running the code I get an error

error: Cannot figure out how to save this field into database. You can consider adding a type converter for it.
    private final java.util.List<com.example.ledger.Database.transaction> transactions = null;

                                                                          ^D:\flutterProjects\Ledger\app\build\tmp\kapt3\stubs\debug\com\example\ledger\Database\account.java:10: error: Cannot find setter for field.

    private final int accountId = 0;

D:\flutterProjects\Ledger\app\build\tmp\kapt3\stubs\debug\com\example\ledger\Database\accountsDao.java:19: warning: The query returns some columns [account_id, name, transaction] which are not used by com.example.ledger.Database.transaction. You can use @ColumnInfo annotation on the fields to specify the mapping. com.example.ledger.Database.transaction has some fields [amount, description, date] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: account_id, name, transaction. Fields in com.example.ledger.Database.transaction: amount, description, date.

    public abstract java.util.List<com.example.ledger.Database.transaction> getTransaction(int id);

                      ^D:\flutterProjects\Ledger\app\build\tmp\kapt3\stubs\debug\com\example\ledger\Database\accountsDao.java:19: error: The columns returned by the query does not have the fields [amount,description,date] in com.example.ledger.Database.transaction even though they are annotated as non-null or primitive. Columns returned by the query: [account_id,name,transaction]

    public abstract java.util.List<com.example.ledger.Database.transaction> getTransaction(int id);

                                                                            ^[WARN] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

编辑

accountDao.kt

@Dao
interface accountsDao {
    @Insert
    fun Insert(account: account)

    @Delete
    fun delete(account: account)

    @Query("SELECT * FROM account_table where account_id = :id ")
    fun getTransaction(id:Int):List<transaction>

}

accountDatabase

@Database(entities = [account::class], version = 1, exportSchema = false)

abstract class accountsDatabase : RoomDatabase() {

    abstract val accountdao: accountsDao

    companion object {

        @Volatile
        private var INSTANCE: accountsDatabase? = null


        fun getInstance(context: Context): accountsDatabase? {

            synchronized(this) {
                // Copy the current value of INSTANCE to a local variable so Kotlin can smart cast.
                // Smart cast is only available to local variables.
                var instance = INSTANCE
                // If instance is `null` make a new database instance.
                if (instance == null) {
                    instance = Room.databaseBuilder(
                        context.applicationContext,
                        accountsDatabase::class.java,
                        "saccount_history_database"
                    )

                        .fallbackToDestructiveMigration()
                        .build()
                    // Assign INSTANCE to the newly created database.
                    INSTANCE = instance
                }
                // Return instance; smart cast to be non-null.
                return instance
            }
        }
    }


}

我感觉在Dao文件中会有一些类型转换程序的代码,因为我正在访问List<transaction>

I feel there would some code of type convertor in Dao file because I am accessing List<transaction>

推荐答案

例如,列表,模型,模型列表等.当您想在数据库中存储某些特殊类型的对象时,可以使用类型转换器.将您的自定义对象类型转换为数据库类型已知的类型.这是Room持久性库中最有用的功能.

For example, lists, models, model list etc. When you want to store some special type objects, such as in the database, you can use Type Converters. Converts your custom object type to a type known for database types. This is the most useful feature of the Room persistent library.

account.kt:

@Entity(tableName = "account_table")
data class account(

@PrimaryKey(autoGenerate = true)@ColumnInfo(name="account_id")
val accountId:Int=0,

@ColumnInfo(name="name")
val name:String,

@TypeConverters(Converters::class)
@ColumnInfo(name="transaction")
val transactions:List<transaction>

)

Converters.kt:

class Converters {

    @TypeConverter
    fun fromTransactionList(transaction: List<transaction?>?): String? {
        if (transaction == null) {
            return null
        }
        val gson = Gson()
        val type: Type = object : TypeToken<List<transaction?>?>() {}.type
        return gson.toJson(transaction, type)
    }

    @TypeConverter
    fun toTransactionList(transactionString: String?): List<transaction>? {
        if (transactionString == null) {
            return null
        }
        val gson = Gson()
        val type =
            object : TypeToken<List<transaction?>?>() {}.type
        return gson.fromJson<List<transaction>>(transactionString, type)
    }

}

您可以这样添加:

@Database(entities = [account::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class accountsDatabase : RoomDatabase() 

和接口accountDao:

And interface accountsDao:

@Query("SELECT * FROM account_table where account_id = :id ")
fun getTransaction(id:Int):List<account>

使用:

GlobalScope.launch {
   val db = accountsDatabase.getInstance(applicationContext)
   val accounts = db.accountDao().getTransaction(your_id)
   val transactions = accounts[position].transactions
   transactions?.forEach {
            Log.d("Transaction Description" ,it.description)
        }
}

这篇关于在会议室数据库中保存自定义数据类型列表时如何解决错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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