question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Room database build errors for @Ignore annotation

See original GitHub issue

Hi,

I’m trying to use Room database within a Kotlin project, but I’m having issues with @Ignore annotation.

I have a data class and I’d like to ignore one field. If I add @Ignore to that field, when I build the project I get several errors saying: “Cannot find setter for field” and one: “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).”

I’ve tried to add a new property within User class from GithubBrowserSample and when I build the project I get the same errors.

image

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:6

github_iconTop GitHub Comments

51reactions
marianpercacommented, Nov 29, 2018

You should move the @Ignore field outside of the constructor, like this:

@Entity(primaryKeys = ["id"])
data class User(
        @field:SerializedName("id")
        val id: Int,

        @field:SerializedName("name")
        val name: String,
    
        @field:SerializedName("age")
        val age: Int
) {
    @Ignore
    val testme: String?
}
18reactions
MatthewTighecommented, Sep 19, 2019

Right, but doesn’t that cause the data class to become mutable? Wouldn’t it need to be var testme: String??

I wanted to retain immutability, so I came up with the following solution:

@Entity(primaryKeys = ["id"])
data class User @Ignore constructor(
    @field:SerializedName("id")
    val id: Int,

    @field:SerializedName("name")
    val name: String,

    @field:SerializedName("age")
    val age: Int,

    @Ignore
    val testme: String?
) {
    constructor(id: Int, name: String, age: Int) : this(id, name, age, null)
}

This allows Room to have only a constructor that matches the fields of the entity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Room @Ignore annotation generates compile time error
I have tried to move the fields inside the class but nothing is working. If I remove the @Ignore annotation from error it...
Read more >
Defining data using Room entities - Android Developers
Room supports several types of annotations that make it easier for you to search for details in your database's tables.
Read more >
Room doesn't ignore @Ignore annotated constructor ...
When I try @JvmOverloads I get the build error: Room cannot pick a constructor since multiple constructors are suitable. Try to annotate unwanted...
Read more >
Configuring Entities - CommonsWare
Since the text property is private and has no setter, one could argue that Room might ignore it automatically. Room, instead, generates a...
Read more >
Error in generated Game.java (page 136) - PragProg Customers
java:7: warning: There are multiple good constructors and Room will pick the no-arg constructor. You can use the @Ignore annotation to eliminate ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found