在Android房间持久化库中如何将整个Model对象插入到本身具有另一个列表的表中.
让我告诉你我的意思:
@Entity(tableName = TABLE_NAME)
public class CountryModel {
public static final String TABLE_NAME = "Countries";
@PrimaryKey
private int idCountry;
private List<CountryLang> countryLang = null;
public int getIdCountry() {
return idCountry;
}
public void setIdCountry(int idCountry) {
this.idCountry = idCountry;
}
public String getIsoCode() {
return isoCode;
}
public void setIsoCode(String isoCode) {
this.isoCode = isoCode;
}
/**
here i am providing a list of coutry information how to insert
this into db along with CountryModel at same …Run Code Online (Sandbox Code Playgroud) 我很难将列表项目放到房间里.列表项称为测量值,其类型为Measurement.列表项没有与数据库相关的主键.但如果有必要,我可以为ProductModel添加相同的主键.
这是我到目前为止:
@Entity(tableName = TABLE_NAME)
public class ProductModel {
public static final String TABLE_NAME = "product";
@PrimaryKey
private int idProduct;
private int idCategoryDefault;
@Relation(parentColumn = "idProduct", entityColumn = "idProduct", entity = SortedAttribute.class)
private List<SortedAttribute> sortedAttributes = null;
}
@Entity
public class SortedAttribute {
@PrimaryKey
private int idProduct;
private String reference;
@Embedded
private List<Measurement> measurements = null; //****how do i get this into room ? its a LIST of measurements, not a measurement so calling Embedded i think wont work as …Run Code Online (Sandbox Code Playgroud) 我有一个名为Chacara的类,它有各种属性,包括thumbList,它是一个List <String>
将它保存到ROOM时,我使用类型转换器:
class DbTypeConverter {
@TypeConverter
fun listToString(list: List<String>): String = Gson().toJson(list)
@TypeConverter
fun stringToList(string: String): List<String> {
return Gson().fromJson<List<String>>(string, object: TypeToken<List<String>>() {}.type)
}
}
Run Code Online (Sandbox Code Playgroud)
当我获取整个对象并访问thumbList属性时,它按预期工作:
@Query("SELECT * FROM chacara WHERE id = :id LIMIT 1")
abstract fun getChacaraWithId(id: String): LiveData<Chacara>
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试仅获取thumbList属性时,结果不会转换:
@Query("SELECT thumbUrlList FROM chacara WHERE id = :id")
abstract fun getChacaraThumbList(id: String): LiveData<List<String>>
Run Code Online (Sandbox Code Playgroud)
我还在TypeConverter中添加了一个日志,以确保它没有被调用.
TL; DR:ROOM不调用TypeConverter而不将String转换回List(SELECT'column_name'),但在请求整个对象时工作(SELECT*)
详细日志
error: Cannot figure out how to save this field into database. You can
consider adding a type converter for it.
private final java.util.Date mTime = null;
我有一个字段为的实体
var mStartTime : Date = Date() // java.util.Date
为什么cant Room持久化Date对象?什么可以是日期最好的转换器?
实体和Pojos必须具有可用的公共构造函数.您可以拥有一个空构造函数或其参数与字段匹配的构造函数(按名称和类型)
我将房间整合到我现有的项目中.在使用@Entity标记注释实现Parcelable的POJO并进行必要的更改时,会收到此错误.我已经有一个空的构造函数.任何帮助,将不胜感激.
@Entity(tableName = "Departments")
public class Department implements Parcelable {
@PrimaryKey(autoGenerate = true)
private Integer primaryId;
private Integer id;
private String departmentName;
private String logoUrl;
@Embedded
private ArrayList<Template> templateList;
public Department() {
}
protected Department(Parcel in) {
this.primaryId = (Integer) in.readSerializable();
this.departmentName = in.readString();
this.logoUrl = in.readString();
this.id = (Integer) in.readSerializable();
this.templateList = in.createTypedArrayList(Template.CREATOR);
}
public static final Creator<Department> CREATOR = new Creator<Department>() {
@Override
public Department createFromParcel(Parcel in) {
return new Department(in);
}
@Override
public Department[] newArray(int size) …Run Code Online (Sandbox Code Playgroud) App Database的Items表的价格为Long数据类型为Long。Db版本= 1
CREATE TABLE items (_id INTEGER PRIMARY KEY AUTOINCREMENT,item_id
INTEGER,title TEXT,price LONG, UNIQUE (item_id) ON CONFLICT IGNORE)
Run Code Online (Sandbox Code Playgroud)
尝试迁移到Room时遇到以下问题
java.lang.IllegalStateException: Migration didn't properly handle items(moka.pos.test.data.entity.Item).
Expected : price=Column{name='price', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}
Found : price=Column{name='price', type='LONG', affinity='1', notNull=false, primaryKeyPosition=0}
Run Code Online (Sandbox Code Playgroud)
这是我的实体类
@Entity(tableName = "items")
public class Item {
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "_id")
private Integer _ID;
@ColumnInfo(name = "item_id")
private Integer id;
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "price") …Run Code Online (Sandbox Code Playgroud) android-room ×6
android ×5
android-architecture-components ×1
entity ×1
java ×1
kotlin ×1
parcelable ×1