Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous getter for Field... Room persistence library

I have the following Entity

public class User {
    @PrimaryKey
    private final long id;

    private String _id;
    private String userName;
    private String email;
}

However when I try to create table for this entity in android using room persistence library, I get the following error.

Error:(18, 20) error: Ambiguous getter for Field(element=_id, name=_id, type=java.lang.String, affinity=TEXT, columnName=_id, parent=null, indexed=false). All of the following match: getId, get_id. You can @Ignore the ones that you don't want to match.

The _id field is included so that I can directly translate the responses from the node.jsapi hooked up to a mongodb database.

like image 621
Giridhar Karnik Avatar asked Aug 13 '17 12:08

Giridhar Karnik


5 Answers

You need to have setter and getter for each private field else you should make them public, another reason that may cause this error is having two getters or setters for a field.

like image 101
Kuti Gbolahan Avatar answered Oct 24 '22 12:10

Kuti Gbolahan


For Room :

In my case

public String getpBSalesTotal() {
    return pBSalesTotal;
}

public void setpBSalesTotal(String pBSalesTotal) {
    this.pBSalesTotal = pBSalesTotal;
}

is the previous POJO class that i want as a table in my database.

I have just changed small "" p "" to capital ""P"" and that solved my error

. Like below

public String getPBSalesTotal() {
    return pBSalesTotal;
}

public void setPBSalesTotal(String pBSalesTotal) {
    this.pBSalesTotal = pBSalesTotal;
}

to generate this type of

POJO form JSON

you can use this LINK. It worked for Room.

like image 6
Wasi Sadman Avatar answered Oct 24 '22 11:10

Wasi Sadman


As per documentation you need to declare getter for every field.

I can see that you have not declared your class with @Entity annotation. Also make sure you are using latest version of library as it is still in development you need to expat some ground breaking changes.

See below example it is working fine for me with below api version of Room.

api 'android.arch.persistence.room:runtime:1.0.0-alpha8'

Or you can use "alpha9" as well. Hope this will solve your problem.

@Entity (tableName = "user")
public class User {

@PrimaryKey
private int id;

private String _id;

@ColumnInfo(name = "user_name")
private String userName;

@NonNull
private String passwrod;

public User(String userName, @NonNull String passwrod) {
    this.userName = userName;
    this.passwrod = passwrod;
}

@Ignore
public User() {
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPasswrod() {
    return passwrod;
}

public void setPasswrod(String passwrod) {
    this.passwrod = passwrod;
}

public String get_id() {
    return _id;
}

public void set_id(String _id) {
    this._id = _id;
}

}

like image 2
Pinakin Kansara Avatar answered Oct 24 '22 10:10

Pinakin Kansara


Return types for getters should be the same as the attribute data type, if you have attribute of data type int, When I found out it fixed my problem.

Good luck.

like image 1
Gpak Avatar answered Oct 24 '22 12:10

Gpak


Me also face the same issue. While I remove the

"_" underscore symbol, from "_id"

then works fine.

public class User {
    @PrimaryKey
    private final long id;

    private String myid;
    private String userName;
    private String email;
}

It works for me and I hope it work for others. Thank You.

like image 1
Rovinsan Avatar answered Oct 24 '22 11:10

Rovinsan