How to fill non column field of entity in room library

前端 未结 4 1381
天命终不由人
天命终不由人 2021-02-01 21:00

I have a class entity as below

@Entity
public class Task {    
    private String name; 
    private Integer ParentId;
    private Integer userId;   
    @Ignore         


        
4条回答
  •  南方客
    南方客 (楼主)
    2021-02-01 21:08

    I faced the same problem a few days ago and had a look at this thread. I am using Kotlin data classes for creating database entities. Given that Kotlin data classes do not play well with inheritance subclassing was not a viable option. My solution was to use the @Embedded keyword in a wrapping class, as follows:

    data class TaskToDisplay(@Embedded
                             var task: Task,
                             var noOfSubTask: Int = 0)
    

    This solution does not create an additional column in the database and most importantly matches all Task's fields with Room's response columns.

提交回复
热议问题