Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room Persistence: Entities and Pojos must have a usable public constructor

I am trying to add a database to my android app through the Room Persistence library and i am getting this error:

error: 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). Tried the following constructors but they failed to match: User(int,java.lang.String,java.lang.String,int,int,int,java.lang.String) -> [param:id -> matched field:unmatched, param:name -> matched field:unmatched, param:gender -> matched field:unmatched, param:age -> matched field:unmatched, param:weight -> matched field:unmatched, param:height -> matched field:unmatched, param:workout -> matched field:unmatched]

Here is my code:

    @Entity
    public class User {

@PrimaryKey
private int userId;
private String userName;
private String userGender;
private int userAge;
private int userWeight;
private int userHeight;
private String workoutPlan;


public User(int id, String name, String gender, int age, int weight, int height, String workout) {

    this.userId = id;
    this.userName = name;
    this.userGender = gender;
    this.userAge = age;
    this.userWeight = weight;
    this.userHeight = height;
    this.workoutPlan = workout;

} ...

Can someone please tell me what i am doing wrong or what i missed?

like image 316
ajonk Avatar asked Dec 20 '18 19:12

ajonk


3 Answers

Kotlin :

@Entity(tableName = "t_article_tabs")
data class WxArticleTabsEntity(
    @ColumnInfo(name = "tabId") @PrimaryKey @SerializedName("id") val id: Int?,
    @ColumnInfo(name = "tabName") @SerializedName("name") val name: String?,
    ...
    @Ignore  @SerializedName("children") val children: List<Any>?,
)

Change to :

@Entity(tableName = "t_article_tabs")
data class WxArticleTabsEntity(
    @ColumnInfo(name = "tabId") @PrimaryKey @SerializedName("id") val id: Int?,
    @ColumnInfo(name = "tabName") @SerializedName("name") val name: String?,
    ...
){
    @Ignore  @SerializedName("children") val children: List<Any>? = null
}
like image 180
javakam Avatar answered Nov 11 '22 18:11

javakam


Please change names of the parameters such that it matches entity attributes.

  public User(int userId, String userName, String userGender, int userAge, int userWeight, int userHeight, String workoutPlan) {
    this.userId = userId;
    this.userName = userName;
    this.userGender = userGender;
    this.userAge = userAge;
    this.userWeight = userWeight;
    this.userHeight = userHeight;
    this.workoutPlan = workoutPlan;
  } ...

For persistance, it uses JavaBeans conventions in Room. For more information: https://developer.android.com/training/data-storage/room/defining-data#java

like image 34
Pradip Karki Avatar answered Nov 11 '22 18:11

Pradip Karki


you can also try this:-

@Ignore
public User(int userId, String userName, String userGender, int userAge, int 
      userWeight, int userHeight, String workoutPlan ) {
   this.userId = userId;
   this.userName = userName;
   this.userGender = userGender;
   this.userAge = userAge;
   this.userWeight = userWeight;
   this.userHeight = userHeight;
   this.workoutPlan = workoutPlan;
 }

 public User(String userName, String userGender, int userAge, int 
      userWeight, int userHeight, String workoutPlan) {
   this.userName = userName;
   this.userGender = userGender;
   this.userAge = userAge;
   this.userWeight = userWeight;
   this.userHeight = userHeight;
   this.workoutPlan = workoutPlan;
 }
like image 29
Nathani Software Avatar answered Nov 11 '22 19:11

Nathani Software