Search code examples
androidandroid-sqlitepersistent-storage

How to store only table with primitive type of Long using Room?


I have list of Ids ,type of Long which is primaryKey as well,I want to store in DB, If I'm storing like this

 @Dao
public interface UserId {
 @Query("SELECT * FROM ids")
Flowable<List<Long>> allIds();


 @Insert(onConflict = OnConflictStrategy.REPLACE)
 List<Long> insert(List<Long> Ids);}

error: Type of the parameter must be a class annotated with @Entity or a collection/array of it. It means I need to have a class with only one field type of Long? which I should annotate with @Entity


Solution

  • @Insert(onConflict = OnConflictStrategy.REPLACE)
    List<Long> insert(List<Long> Ids);
    

    A Long is not an entity. You cannot use @Insert to insert Long objects, in part because there is no table for Long values.

    You need to either:

    • @Insert actual entities, or

    • Create a @Query method, with SQL that explains what you plan on doing with those Long values

    Also, I suspect that your existing @Query will not work, now or in the future, as Room will not know what Long you are expecting. It might work if you happen to have only one suitable field in whatever entity defines the userfavoritestore table. But once you have 2+ integer fields, Room will not know which one you want. Replace * in the @Query with the actual column name you want to return.