我有一个User实体,该实体存储自定义对象通知的列表。 尽管我使用了TypeConverters,但查询仍然出现错误。
我的用户实体(不包括getter和setter)
@Entity(tableName = "User")
public class User {
@PrimaryKey(autoGenerate = true)
private int userId;
@ColumnInfo(name = "username")
private String username;
@ColumnInfo(name = "number")
private String number;
@TypeConverters(Converters.class)
private List<Notifications> notifications;
public User(String username, String number, List<Notifications> notifications) {
this.username = username;
this.number = number;
this.notifications = notifications;
}
}
userDao界面
@Dao
public interface UserDao {
@Query("SELECT * FROM User")
List<User> getAll();
@Query("SELECT COUNT(*) from User")
int countUsers();
@Query("SELECT notifications from User where userId LIKE :userId")
List<Notifications> getNotifications(int userId);
@Insert
void insert(User... users);
@Delete
void delete(User... users);
@Update
void update(User... users);
}
转换器类
public class Converters {
@TypeConverter
public static List<Notifications> fromString(String value) {
Type listType = new TypeToken<List<Notifications>>() {}.getType();
List<Notifications> notifications = new Gson().fromJson(value,listType);
return notifications;
}
@TypeConverter
public static String listToString(List<Notifications> list) {
Gson gson = new Gson();
return gson.toJson(list);
}
}
通知类(不包括getter和setter)
public class Notifications {
private String app;
private String type;
private String time;
public Notifications(String app, String type, String time) {
this.app = app;
this.type = type;
this.time = time;
}
}
AppDatabase类
@Database(entities = {User.class}, version = 1)
@TypeConverters({Converters.class})
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract UserDao userDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "user-database")
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
我遇到了错误
error: Not sure how to convert a Cursor to this method's return type
和
The query returns some columns [notifications] which are not use by com.example.ark.example.database.Notifications. You can use @ColumnInfo annotation on the fields to specify the mapping. com.example.ark.example.database.Notifications has some fields [app, type, time] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: notifications. Fields in com.example.ark.example.database.Notifications: app, type, time.
由于类型转换器对我来说看起来不错,所以不确定我要怎么做。
答案 0 :(得分:0)
尝试在ls *PackageName* | sort | tail -1 |
xargs tail -500f
下添加@ColumnInfo(name = "notifications")
答案 1 :(得分:0)
尝试在所有字段中添加@ColumnInfo
和@SerializedName
注释,如下所示。
@TypeConverters(Converters.class)
@ColumnInfo(name = "notifications")
@SerializedName("notifications")
private List<Notifications> notifications;
也添加
通知模型:
@SerializedName("app")
private String app;
@SerializedName("type")
private String type;
@SerializedName("time")
private String time;
答案 2 :(得分:0)
请将@ColumnInfo(name = "........")
添加到List<Notifications> notifications
条目