I am new to using room persistence and I have this error whenever I try running my code. It shows no error when editing but I get build error on gradle. It failed me that I had to copy paste codes but none seemed to work. The error is below
error: An entity must have at least 1 field annotated with @PrimaryKey
How do I work it out? My code is below;
package com.revosleap.dummy.DatabaseMov;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
@Entity
public class TodoListItem {
@Ignore
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "time")
private String time;
@ColumnInfo(name="title")
private String title;
public TodoListItem(){
}
public TodoListItem(String time, String title) {
this.time = time;
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
You shouldn't have primary key as private or static
Change private int id; this line to
@PrimaryKey
public int id;
more details Room entities
You should not use @Ignore with @PrimaryKey
@Ignore annotation Ignores the marked element from Room's processing logic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With