简体   繁体   中英

How to properly annotate object variables that are part of primary key in Room entity?

I am getting an error: "You must annotate primary keys with @NonNull. "name" is nullable."

I do not understand what the source of this error is as I am very clearly annotating my String variable "name" with @NonNull that is declared as part of the composite primary key. Am I setting up my composite primary key incorrectly?

@Entity(tableName = "ScavItems",
        primaryKeys = { "scavHuntID", "name" },
        foreignKeys = {
                @ForeignKey(entity = ScavHunt.class,
                        parentColumns = "shID",
                        childColumns = "scavHuntID")
        })
public class ScavItem {
    @NonNull private int scavHuntID;
    @NonNull private String name;

    @ColumnInfo(name = "found")
    private boolean found;

    public ScavItem(int scavHuntID, String name){
        this.scavHuntID = scavHuntID;
        this.name = name;
        found = false;
    }

    public String getName(){ return this.name; }
    public boolean getFound(){ return this.found; }
    public int getScavHuntID(){ return this.scavHuntID; }
    public void setFound(boolean found){ this.found = found; }
}

I figured it out. I was importing the incorrect NonNull package. This is the correct import:

import android.support.annotation.NonNull;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM