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

前端 未结 24 1805
长发绾君心
长发绾君心 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:20

    As stated in Room Database Entity:

    Each entity must either have a no-arg constructor or a constructor whose parameters match fields (based on type and name).

    So adding an empty constructor and annotating your parameterized constructor with @Ignore will solve your problem. An example:

    public class POJO {
    
        long id;
    
        String firstName;
    
        @Ignore
        String lastName;
    
        public POJO() {
        }
    
        @Ignore
        public POJO(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    
        // getters and setters
        // ...
    
    }
    

提交回复
热议问题