Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 767
ahmed galal Avatar asked Oct 05 '18 13:10

ahmed galal


1 Answers

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.

like image 95
ahmed galal Avatar answered Nov 15 '22 19:11

ahmed galal