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

前端 未结 24 1808
长发绾君心
长发绾君心 2020-12-01 09:05

I\'m converting a project to Kotlin and I\'m trying to make my model (which is also my entity) a data class I intend to use Moshi to convert the JSON responses from the API<

24条回答
  •  醉酒成梦
    2020-12-01 09:16

    Like it's said in the Room docs, you are required to make an empty public constructor. At the same time, if you want to declare other custom constructors, you must add @Ignore annotation.

    @Entity
    public class CartItem {
        @PrimaryKey
        public int product_id;
        public int qty;
    
        public CartItem() {
        }
    
        @Ignore
        public CartItem(int product_id, int count) {
            this.product_id = product_id;
            this.qty = count;
        }
    }
    

提交回复
热议问题