如何在Android Room中@Ignore Sqlite实体列

ql3eal8s  于 2023-06-20  发布在  Android
关注(0)|答案(3)|浏览(126)

我当前的Android应用程序需要从本地Sqlite数据库中获取带有计数字段的行。
我意识到我需要让我的DO实体定义这个计数字段并@Ignore(ed)
我在google上搜索了一下,找到了这个page

@Entity
data class User(
    @PrimaryKey val id: Int,
    val firstName: String?,
    val lastName: String?,
    @Ignore val picture: Bitmap?
)

然而,当我将这段Kotlin代码复制并粘贴到我的应用程序中时,它无法构建,并出现以下异常

database_room:kaptStagingDebugKotlin FAILED
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:7: error: Entities and POJOs must have a usable public constructor. You can have an empty constructor or a constructor whose parameters match the fields (by name and type).
public final class User {
             ^
  Tried the following constructors but they failed to match:
  User(int,java.lang.String,java.lang.String,android.graphics.Bitmap) -> [param:id -> matched field:id, param:firstName -> matched field:firstName, param:lastName -> matched field:lastName, param:picture -> matched field:unmatched]
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:9: error: Cannot find setter for field.
    private final int id = 0;
                      ^
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:11: error: Cannot find setter for field.
    private final java.lang.String firstName = null;
                                   ^
e: /Users/rossulbricht/projects/silkroad/database_room/build/tmp/kapt3/stubs/stagingDebug/org/dread/pirate/database_room/model/User.java:13: error: Cannot find setter for field.
    private final java.lang.String lastName = null;
                                   ^
w: [kapt] Incremental annotation processing requested, but support is disabled because the following processors are not incremental: androidx.room.RoomProcessor (DYNAMIC).

> Task :networking:mergeStagingDebugNativeLibs
> Task :android_idplus:generateStagingDebugRFile
> Task :networking:processStagingDebugManifest

FAILURE: Build failed with an exception.

我的问题是
如何忽略Kotlin数据类房间实体中的列?
为什么google android的例子不起作用?
我在用

Android Studio 3.5
Build #AI-191.8026.42.35.5791312, built on August 9, 2019
JRE: 1.8.0_202-release-1483-b49-5587405 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.6

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://sdk.smartlook.com/android/release"
        }
    }
}

ext {
    compileSdkVersion = 28
    targetSdkVersion = 28
    minSdkVersion = 21
    supportLibVersion = '1.1.0-beta01'

    coreKtx = '1.2.0-alpha04'
    materialMaterial = '1.1.0-alpha10'
    legacySupportV4 = "1.0.0"
    recyclerView = '1.1.0-beta04'
    lifecycleExtensions = '2.2.0-alpha04'
    playServicesAuthVersion = "16.0.1"
    archLifecycleVersion = '2.2.0-alpha04'
    roomVersion = '2.2.0-rc01'
    archPagingVersion = '2.1.0'
    archWorkerRuntimeVersion = '2.3.0-alpha01'
    constraintLayoutVersion = '2.0.0-beta2'
    browserVersion = '1.0.0'

    appCompatVersion = '1.1.0'

    junitVersion = "4.12"
    testRunnerVersion = '1.1.0-alpha4'
    espressoTestCoreVersion = '3.1.0-alpha4'
    uiautomatorVersion = '2.2.0-alpha4'

    leakcanaryVersion = "1.6.1"
    commonsTextVersion = '1.7'

    rxbinding2Version = "2.2.0"
    picassoVersion = "2.71828"
    retrofit2Version = '2.6.1'
    rxAndroidVersion = '2.1.1'
    rxJavaVersion = '2.2.11'
    jacksonKotlinVersion = '2.10.0.pr1'
    okhttp3Version = '4.1.1'
    okioVersion = '2.4.0'
    jakewRxrelay2Version = '2.1.0'

    daggerVersion = '2.24'

    numbusJoseJwtVersion = '7.1'
    interComVersion = "5.4.1"

    guavaAndroidVersion = "28.1-android"
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

allprojects {
    gradle.projectsEvaluated {
        tasks.withType(JavaCompile) {
            options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
        }
    }
}
e4eetjau

e4eetjau1#

这里

@Entity
data class User(
@PrimaryKey val id: Int,
val firstName: String?,
val lastName: String?,
@Ignore val picture: Bitmap?
)

修改它以便

@Entity
data class User(
@PrimaryKey val id: Int,
val firstName: String?,
val lastName: String?,
@Ignore val picture: Bitmap?
){
constructor():this(id=null,firstName=null,lastName=null,picture=null) }

然后另一个错误将出现,因为没有setter,所以将val替换为var

@Entity
data class User(
@PrimaryKey var id: Int,
var firstName: String?,
var lastName: String?,
@Ignore val picture: Bitmap?
){
constructor():this(id=null,firstName=null,lastName=null,picture=null) }

这是我用来放东西的架子

implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-rxjava2:$room_version"
implementation "androidx.room:room-ktx:$room_version"

在顶部添加apply plugin: 'kotlin-kapt'

ext.room_version = '2.2.0-rc01'
yebdmbv4

yebdmbv42#

该实体的值应该是带有构造函数的var data: String?

@Entity
data class User(
    @PrimaryKey val id: Int,
    var firstName: String?,
    var lastName: String?,
    @Ignore var picture: Bitmap?
){
  constructor: this(firstName = "", lastName = "")
}

当然,你可以做val s,但你应该添加构造函数。
或者val s:

@Entity
data class User(
    @PrimaryKey val id: Int,
    var firstName: String,
    var lastName: String,
    @Ignore var picture: Bitmap
)

实际上,@Ignore与此无关。更多信息,请访问refer this article
你不可能同时拥有val s和null。房间不允许。如果你想让你的变量可以为空,坚持使用var,并如上所述向数据类添加构造函数。
为什么谷歌Android的例子不工作?如果您检查您的数据类和您引用的页面中的代码,您将注意到这种差异。

twh00eeo

twh00eeo3#

这样也可以

@Entity
data class User(
    @PrimaryKey val id: Int = 0,
    val firstName: String? =null,
    val lastName: String?=null,
){
    @Ignore var picture: Bitmap? = null
}
val u = User().apply{picture=Bitmap.build()}

相关问题