简体   繁体   中英

error: Entities and Pojos must have a usable public constructor - Java

Whenever I try to compile the app I get 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). - java.util.List

SystemMessagesEntity.java :

    @Entity(tableName = "system_messages")
    @TypeConverters(ReplyMessages.class)
    public class SystemMessageEntity {
        @PrimaryKey
        @NonNull
        public String messageId;
        public String title;
        public String body;
        public String color;
        public Integer date;
        public String icon;
        public String ad;
        public Integer isRead;
        public String ip;
        String id; //user_id
        @Embedded(prefix = "reply_")
        private List<ReplyMessage> reply_message;
    
        @Ignore
        public SystemMessageEntity() {
        }
    
        public SystemMessageEntity(String id, @NonNull String messageId, String title, String body, String color, Integer date, String icon, String ad, Integer isRead, String ip, List<ReplyMessage> reply_message) {
            this.id = id;
            this.messageId = messageId;
            this.title = title;
            this.body = body;
            this.color = color;
            this.date = date;
            this.icon = icon;
            this.ad = ad;
            this.isRead = isRead;
            this.ip = ip;
            this.reply_message = reply_message;
        }
    //getters and setters
}

ReplyMessages.java :

@TypeConverters(ReplyMessages.class)

public class ReplyMessage {

private String body = "";
private String userId = "";
private Integer date = 0;
private String id = "";
private String messageId = "";


@Ignore
public ReplyMessage() {
}

public ReplyMessage(String body, String userId, Integer date, String id, String messageId) {
    this.body = body;
    this.date = date;
    this.id = id;
    this.messageId = messageId;
    this.userId = userId;
}

And this is the TypeConverter :

public class ReplyMessages {

    @TypeConverter
    public static String ListToJson(List<ReplyMessage> replyMessages) {
        if(replyMessages == null) return null;
        Type type = new TypeToken<List<ReplyMessage>>() {}.getType();
        String json = new Gson().toJson(replyMessages, type);
        Log.i("JSON", "toJson: " + json);
        return replyMessages.isEmpty() ? null : json;
    }

    @TypeConverter
    public static List<ReplyMessage> JsonToList(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<ReplyMessage>>() {}.getType();
        List<ReplyMessage> replyMessages = gson.fromJson(json, type);
        return replyMessages;
    }
}

I want to get a JSON data from API using Retrofit and store it in a database, The JSON data have an array of objects ( MessageReply ) inside the parent array of objects ( SystemMessagesEntity ).

I have tried everything and searched in almost every question in StackOverflow, but I haven't found anything.

Edit : When I used the SystemMessageEntity Entity alone, The database worked fine. But when I added the ReplyMessage Entity, The problem appeared.

Edit 2: The problem has been solved by removing the List<> of the ReplyMessage. I don't know why the list is not working although I'm using a converter.

I have solved the problem. Well, it seems like I can't use @Embedded with @TypeConverter mostly because the converter returns a String and it's primitive not a POJO class. Anyway, I removed the @Embedded annotation and it worked fine.

Android Room needs an empty constructor, so remove the @Ignore annotation from the empty constructor and place the same on the constructor with parameters.

 public SystemMessageEntity() {}

 @Ignore
 public SystemMessageEntity(String id, @NonNull String messageId, ...) {
     // ... initializing the params
 }

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