Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Realm understand LinkingObjects

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

like image 323
Sam Avatar asked May 03 '17 11:05

Sam


People also ask

What is realm database in Android with Kotlin?

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.

How to add data to an object in a realm?

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.

What is real realm for Android?

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.

Can entity objects in an object-relational mapping library reference each other?

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.


Video Answer


1 Answers

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;
    }
}
like image 167
EpicPandaForce Avatar answered Oct 06 '22 19:10

EpicPandaForce