Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room @Query error: Cannot find method parameters

in my project I have a library module and an application module using it. In both modules I have the same gradle dependencies on Android Architecture Components library:

// ViewModel and LiveData
implementation "android.arch.lifecycle:extensions:1.0.0"
implementation "android.arch.lifecycle:common-java8:1.0.0"

// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

In my library module I have defined a User entity

@Entity(tableName = "users",
    indices = {@Index(value = {"firstName", "lastName"})})
public class User {

public enum ROLE {
    ...
}

public enum FEEDBACK_LEVEL {
    ...
}

@PrimaryKey
public int id;

@TypeConverters(UserConverters.class)
ROLE role;

@TypeConverters(UserConverters.class)
FEEDBACK_LEVEL feedbackLevel;

public String firstName;

public String lastName;


public int getId() {
    return id;
}

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

public ROLE getRole() {
    return role;
}

public void setRole(ROLE role) {
    this.role = role;
}

public FEEDBACK_LEVEL getFeedbackLevel() {
    return feedbackLevel;
}

public void setFeedbackLevel(FEEDBACK_LEVEL feedbackLevel) {
    this.feedbackLevel = feedbackLevel;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}
}

and the related DAO interface

@Dao
public interface UserDAO {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertUser(User ... u);

    @Query("select * from users where users.id = :userId")
    LiveData<User> getUser(int userId);
}

In my application module I've created my database in which I'm using the entity defined in the library project

@Database(entities = {User.class}, version = 1)
public abstract class TDatabase extends RoomDatabase{
    private static TDatabase sInstance;

    public static TDatabase getInstance(final Context c) {
        if(sInstance == null)
            sInstance = Room.databaseBuilder(c, TDatabase.class, "t_db").build();
        return sInstance;
    }

    public abstract UserDAO userDao();
}

The problem is that when I try to refer to a method parameter in a @Querystatement using its name I get the following error

Error:Each bind variable in the query must have a matching method parameter. Cannot find method parameters for :userId.

If I change the @Query from

@Query("select * from users where users.id = :userId")
LiveData<User> getUser(int userId);

to

@Query("select * from users where users.id = :arg0")
LiveData<User> getUser(int userId);

everything works fine.

Am I doing some mistakes? Why I'm getting this error? I've googled for a solution but I found only results referring to Kotlin while I'm using Java.

like image 275
hara Avatar asked Jan 04 '18 17:01

hara


2 Answers

This issue reported at https://issuetracker.google.com/issues/68118746 and fixed in version 1.1.0-alpha3, but only for Kotlin code since parameter names are always stored in Kotlin classes metadata.

For Java, there is only workaround with :arg0 until annotation like NamedArg will be added to Room.

like image 126
KursoR Avatar answered Sep 24 '22 03:09

KursoR


I think you should use it like that:

@Query("select * from users where id = (:userId)")
LiveData<User> getUser(int userId);
like image 29
Mycoola Avatar answered Sep 23 '22 03:09

Mycoola