Android房间数据库:自动生成的bind()函数引发不兼容类型错误



我正在为Android开发Room数据库。我对此没有太多经验,所以我一直在遵循一些指南,试图使其适应我的需要。在构建时,我从自动生成的Dao-Impl文件中得到以下错误:error: incomparable types: boolean and <null> _tmp = _tmp_1 == null ? null : (_tmp_1 ? 1 : 0);

引发此错误的生成代码是:

@Override
public void bind(SupportSQLiteStatement stmt, Product value) {
final Integer _tmp;
final boolean _tmp_1;
_tmp_1 = value.getId() != 0;
_tmp = _tmp_1 == null ? null : (_tmp_1 ? 1 : 0);

在所有的Dao实现中都存在同样的错误,所以我不确定这里的问题是什么

我有三个道,这是他们的代码:

import androidx.lifecycle.LiveData
import androidx.room.*
@Dao
interface ProductDao {
@Query("SELECT * FROM product_table")
fun getAll(): List<Product>
@Transaction
@Query("SELECT * FROM product_table WHERE id = :id LIMIT 1")
fun getProductById(id: Int): Product
@Transaction
@Query("SELECT * FROM product_table WHERE name LIKE :name")
fun getProductByName(name: String): Product
@Query("SELECT id FROM product_table WHERE name LIKE :name")
fun getId(name: String): Int
@Transaction
@Insert(entity = Product::class)
suspend fun insert(product: Product)
@Transaction
@Delete
suspend fun drop(product: Product)
@Query("DELETE FROM product_table")
suspend fun dropAll()
}
import androidx.room.*
@Dao
interface ReportDao {
@Query("SELECT * FROM report_table WHERE reportId = :reportId LIMIT 1")
fun getReport(reportId: Int): Report
@Insert
suspend fun insert(report: Report)
@Delete
suspend fun drop(report: Report)
}
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
@Dao
interface UserDao {
@Query("SELECT * FROM user_table WHERE id = :id LIMIT 1")
fun getUserById(id: Int): User
@Insert
suspend fun insert(user: User)
@Delete
suspend fun drop(user: User)
}

错误来自生成的构造函数到实现类:

this.__insertionAdapterOfProduct = new EntityInsertionAdapter<Product>(__db) {
@Override
public String createQuery() {
return "INSERT OR ABORT INTO `product_table` (`id`,`name`,`parts`) VALUES (nullif(?, 0),?,?)";
}
@Override
public void bind(SupportSQLiteStatement stmt, Product value) {
final Integer _tmp;
final boolean _tmp_1;
_tmp_1 = value.getId() != 0;
_tmp = _tmp_1 == null ? null : (_tmp_1 ? 1 : 0);
if (_tmp == null) {
stmt.bindNull(1);
} else {
stmt.bindLong(1, _tmp);
}
if (value.getName() == null) {
stmt.bindNull(2);
} else {
stmt.bindString(2, value.getName());
}
final String _tmp_2;
_tmp_2 = __dataConverter().fromStringList(value.getParts());
if (_tmp_2 == null) {
stmt.bindNull(3);
} else {
stmt.bindString(3, _tmp_2);
}
}
};

我想这个问题已经解决了,但如果有人稍后寻求这些信息,我无论如何都会发布。

因此,至少在我的情况下,问题不在任何Dao文件中,而是在实体中。然而,androidstudio提供的错误消息是非常没有信息的,并且丝毫没有指向实体类。

问题是,我的实体有一个双倍的值,而且它像往常一样有getter。我试图通过将双精度舍入为最接近的整数并返回该值而不是双精度值来返回一个更便于打印的值。我不知道的是,Dao实现文件(自动生成(使用了相同的getter,并期望得到双倍的回报。长话短说,如果你对你的实体价值观做了一些有趣的事情,不要在胡思乱想。

TLDR;确保实体类getter返回正确的类型

最新更新