如何使用Android Room @Ignore Sqlite实体列

时间:2019-09-27 07:11:23

标签: android kotlin android-room

我当前的Android应用程序需要从本地Sqlite数据库中获取带有count字段的行。

我意识到我需要让我的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"
        }
    }
}

2 个答案:

答案 0 :(得分:1)

这里

@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) }

然后会出现另一个错误,因为没有设置器,因此将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'

答案 1 :(得分:0)

该实体的值应为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,但是您应该添加构造函数。

或者只是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和null。房间不允许这样做。如果您希望变量为可空值,请坚持使用var并按如上所述将构造函数添加到数据类中。

  

为什么Google android示例不起作用?   如果您从引用的页面检查数据类和代码,则会注意到这种差异。