房间是否忽略“实体发送更新”表中为空的数据? 我想更新实体中的列的一部分,但是我很想将新数据存储在实体类中,然后将其发送到DAO中的更新,是否替换该行中的其他数据?!
例如,如果实体类为:
@Entity(tableName = "ITEM_TABLE")
public class Item
{
@PrimaryKey
@NotNull
@ColumnInfo(name = "ID")
public int id;
@ColumnInfo(name = "COLUMN_TITLE")
public String title;
@ColumnInfo(name = "COLUMN_WEIGHT")
public String weight;
}
并请求更新,例如:
Item item = new Item();
item.title = "new Title";
item.id = 2;
ItemDAO.update(item);
如果存在id = 2的实体且其权重=“ 250”,如果我以这种方式更新它会发生什么?!
答案 0 :(得分:0)
尝试定义自己的DAO。
@Dao
public interface IDaoAccessStuff {
@Query("UPDATE SET COLUMN_TITLE=:text WHERE id=:id")
void UpdateColumnById (String text, int id);
...
@Insert
void insertStuff (Stuffstuff);
@Update
void updateExercise (Exercises exercise);
}
然后:
@Database (entities = {Stuff.class}, version = 1, exportSchema = false)
public abstract class AStuffDatabase extends RoomDatabase {
public abstract IDaoStuffExercise daoAccess();
}
最后:
private AStuffDatabase aStuffDatabase;
...
RoomDatabase.Builder<AExerciseDatabase> builder;
builder = Room.databaseBuilder(getApplicationContext(),
AStuffDatabase.class,
YOUR_DATABASE_NAME);
aStuffDatabase = builder.fallbackToDestructiveMigration().build();
...
aStuffDatabase.daoAccess().updateStuff(yourString, theId);
此外,除非您指定该列不应接受空值,如下所示:
@ColumnInfo(name = "COLUMN_TITLE")
@NonNull
private String title;