I need help on LinkingObjects in Realm. Please look at these simple codes:
public class Product extends RealmObject
{
@PrimaryKey
private int prodId;
@Required
private String name;
private RealmList<ProductItem> productItems;
@LinkingObjects("productParent")
private final RealmResults<ProductItem> linkProductItems = null;
...
...
...
}
public class ProductItem extends RealmObject
{
@PrimaryKey
private String primaryKey;
private int prodId;
private int prodItemId;
private String itemCode;
private double price;
private Product productParent;
...
...
...
public Product getProductParent()
{
return productParent;
}
}
Then, I added the sample data by doing this:
realm.beginTransaction();
Product prod = new Product();
prod.setProdId(1);
prod.setName("Test");
prod = realm.copyToRealm(prod);
ProductItem prodItem = new ProductItem();
prodItem.setProdId(prod.getProdId());
prodItem.setProdItemId(1);
prodItem.setItemCode("00231");
prodItem.setPrice(9.95);
prodItem.getProductItems().add(realm.copyToRealm(prodItem));
realm.commitTransaction();
Now, from what I understand LinkingObjects allows you to refer back to your parent? But the following code will fails:
String sOutput = "";
for (ProductItem prodItem : realm.where(ProductItem.class).findAll())
sOutput += prodItem.getProductParent().getName() + "\n";
The problem is bqItem.getProductParent() is NULL. My question is, have I done LinkingObjects correctly? If not, can you help me?
Thanks
It stores data in a native object format whereby objects are represented in the database in a universal table-based format. This makes Realm database easy to use in different languages and platforms. In this tutorial, we will focus on the Realm database in Android with Kotlin.
With the object that we have created, we can add data by accessing its attribute. We afterward call the realm.insertOrUpdate (object) method to commit the new or updated data into the database.
Realm is faster and has tons of new features such as JSON support, an easy-to-use API, notifications when the data changes, cross-platform support, faster querying, and it is fully encrypted all of which make mobile development easy. In this tutorial, we’ll explore the fundamentals of Realm for Android.
Even though most object-relational mapping libraries allow entity objects to reference each other, Room explicitly forbids this. To learn about the technical reasoning behind this decision, see Understand why Room doesn't allow object references.
You are looking for
public class Product extends RealmObject
{
@PrimaryKey
private int prodId;
@Required
private String name;
private RealmList<ProductItem> productItems;
//@LinkingObjects("productParent")
//private final RealmResults<ProductItem> linkProductItems = null;
...
...
...
}
public class ProductItem extends RealmObject
{
@PrimaryKey
private String primaryKey;
private int prodId;
private int prodItemId;
private String itemCode;
private double price;
//private Product productParent;
@LinkingObjects("productItems") // <-- !
private final RealmResults<Product> productParents = null; // <-- !
...
...
...
public RealmResults<Product> getProductParents() // <-- !
{
return productParents;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With