简体   繁体   中英

Room query return LiveData of parent class type

I'm having three clases:

  • Task (parent class)
  • ProgressTask (subclass of Task)
  • TodolistTask (subclass of Task)

ProgressTask and TodolistTask differ by few parameters.

Both ProgressTask and TodolistTask are entities in my Room db. I want to have TaskDao in which I would like to call this query:

@Query("SELECT * FROM  progressTask_table UNION SELECT * FROM todolistTask_table")
LiveData<List<Task>> getAllTasks();

is it possible? Now I'm getting a reasonable error saying that SELECTs to the left and right have different number of columns.

You have to call out the individual columns in you query when using a union.

You objects most likely have different columns. Example ProgressTask has a date that TodolistTask doesn't.

If you call out each column in the base class in both select parts of your query it will work.

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