我的模型类为RealmObject,调用ProductsDetailInformation
,在此模型中我有单个字段
private RealmList<ProductSlideShowImages> slideShowImages;
我希望从ProductSlideShowImages
模型中获取此产品图片,但我的此测试返回[0]
,例如:
ProductsDetailInformation = [
{id: 57f72549-53e1-4177-9e52-80b8421559b7},
{marketId: bc76b7a5-3166-11e6-b8fe-ec9a74f8851a},
{productId: bc76b7a5-3166-11e6-b8fe-ec9a74f8851a-123456},
{weight: 10},
{cost: 1000},
{color: blue},
{madeIn: china},
{gender:},
{sex:},
{quality: low},
{size: 42},
{sizeModel: xxl},
{count: 10},
{rating: 2},
{description: my description},
{created_at: 2016 - 07 - 17},
{updated_at: 2016 - 07 - 14},
{slideShowImages: RealmList < ProductSlideShowImages > [0]}
]
在marketId
和productId
中都有相同的数据。
ProductsDetailInformation
上课:
public class ProductsDetailInformation extends RealmObject{
@PrimaryKey
private String id;
private String marketId;
private String productId;
private String weight;
private String cost;
private String color;
private String madeIn;
private String gender;
private String sex;
private String quality;
private String size;
private String sizeModel;
private String count;
private String rating;
private String description;
private String created_at;
private String updated_at;
/**
* RelationShip columns with ProductSlideShowImages table
*/
private RealmList<ProductSlideShowImages> slideShowImages;
/*SETTER AND GETTER*/
}
ProductSlideShowImages
上课:
public class ProductSlideShowImages extends RealmObject{
@PrimaryKey
private String id;
private String marketId;
private String productId;
private String imageFileName;
private String created_at;
private String updated_at;
/*SETTER AND GETTER*/
}
使用查询获取结果:
List<ProductsDetailInformation> productsDetails =
realm.where(ProductsDetailInformation.class).equalTo("marketId",marketUUid).findAll();
ProductSlideShowImages
模型和ProductsDetailInformation
不会填充在一起,它们会填补不同的地点和时间
修改
以下trasaction我将信息保存到ProductSlideShowImages模型
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ProductSlideShowImages slideShowImages = new ProductSlideShowImages();
try {
//@formatter:off
slideShowImages.setId ( UUID.randomUUID().toString() );
slideShowImages.setMarketId ( jsonObject.getString ( "marketId" ));
slideShowImages.setProductId ( jsonObject.getString ( "productId" ));
slideShowImages.setImageFileName ( jsonObject.getString ( "imageFileName" ));
slideShowImages.setCreated_at ( jsonObject.getString ( "created_at" ));
slideShowImages.setUpdated_at ( jsonObject.getString ( "updated_at" ));
//@formatter:on
realm.copyToRealm(slideShowImages);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
并且在其他方法中使用以下事务我将数据保存到ProductsDetailInformation
模型:
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
ProductsDetailInformation products = new ProductsDetailInformation();
try {
//@formatter:off
products.setId ( UUID.randomUUID().toString() );
products.setMarketId ( jsonObject.getString ( "marketId" ));
products.setProductId ( jsonObject.getString ( "productId" ));
products.setWeight ( jsonObject.getString ( "weight" ));
products.setCost ( jsonObject.getString ( "cost" ));
products.setColor ( jsonObject.getString ( "color" ));
products.setMadeIn ( jsonObject.getString ( "madeIn" ));
products.setGender ( jsonObject.getString ( "gender" ));
products.setSex ( jsonObject.getString ( "sex" ));
products.setQuality ( jsonObject.getString ( "quality" ));
products.setSize ( jsonObject.getString ( "size" ));
products.setSizeModel ( jsonObject.getString ( "sizeModel" ));
products.setCount ( jsonObject.getString ( "count" ));
products.setRating ( jsonObject.getString ( "rating" ));
products.setDescription ( jsonObject.getString ( "description" ));
products.setCreated_at ( jsonObject.getString ( "created_at" ));
products.setUpdated_at ( jsonObject.getString ( "updated_at" ));
//@formatter:on
realm.copyToRealm(products);
} catch (JSONException e) {
e.printStackTrace();
Log.e("Error ", " parse json objects");
}
}
});
答案 0 :(得分:2)
您必须手动设置关系。
ProductsDetailInformation products = new ProductsDetailInformation();
products.setSlideShowImages(new RealmList<ProductSlideShowImages>());
try {
...
RealmResults<ProductSlideShowImages> slideShows = realm
.where(ProductSlideShowImages.class)
.equalTo("marketId", products.getMarketId())
.findAll();
for(ProductSlideShowImages slideShow : slideShows) { //only 0.89.0+
products.getSlideShowImages().add(slideShow);
}
realm.copyToRealm(products);