Start a new topic

Relational data in android

How to store Relational data in android.

1 person has this question

Hi Vikash,

Have you seen our CTO's post about [data modeling in Kinvey]( "data modeling in Kinvey")? Is there some specific Android topic you wanted more information about?
Hi Caroline,

Thanks for your reply, i seen your CTO's post, and i also see its developer documentation on

But, i am not able to find its proper code, how to store relational data to two different collection using method,

Let's try to pin down where your problems are deriving from. For further investigations could you please provide a few more details?

There are three steps:

1. Define the `KinveyReference` in your `GenericJson`

2. Use the `KinveyReference` constructor to create a new one

3. Use appdata's `get` method to retrieve it, passing in the field name you defined in Step 1.

What issues are you running into?
Hello Edward,

thanks for your response,

we create tw o model class as MyEntity, NewEntity. and created two different collection Collection_1, and Collection_2.

I defined KinveyRefernce in my MyEntity class, and intialize initReference(NewIntity newentityobj)

public void initReference(InvitationEntity myInvitation){

KinveyReference reference = new KinveyReference("TestTableWin", myInvitation.get("_id").toString());

this.invitations = reference;


With this code we can save data to Collection_1, but i want to save data to my second Collection_2 as well.

MyEntity event = new MyEntity();

event.setName("Launch Party");

AsyncAppData myevents = mKinveyClient.appData("Collection_1", MyEntity.class);, new KinveyClientCallback() {


public void onFailure(Throwable e) {

Log.e(TAG, "failed to save event data", e);



public void onSuccess(MyEntity r) {

Log.d(TAG, "saved data for entity "+ r.getName());



`KinveyReference`s are used to create a reference between two entities possibly across collections. So, using your data, you could have a `MyEntity` in `Collection_1`, the contains a reference to a `NewEntity` in `Collection_2`.

So, within your `MyEntity`, there is a `private KinveyReference invitations;`, as well as that `initReferences(...)` method.

First, there are a couple issues with that `initReferences(...)` method. It looks like it takes an `InvitationEntity` instead of a `NewEntity`, and assumes the collection is `TestTableWin` instead of `Collection_2`.

then, in the second code snippet, both a `MyEntity` and a `NewEntity` need to be created, and then call `event.initReference(myNewEntit)`, which will set the reference. Then, after that, call save.

Does that make sense?
Hello Edward,

My Code

In Activity class

SecondEntity eve2=new SecondEntity();

HelloEntity event = new HelloEntity();





eve2.setStatus("new status");


AsyncAppData myevents = mKinveyClient.appData("Collection_1",HelloEntity.class);, new KinveyClientCallback() {


public void onFailure(Throwable e) {}


public void onSuccess(HelloEntity arg0) {}


In My Entity Class 1 : HelloEntity

public void initReference(SecondEntity myInvitation){

KinveyReference reference = new KinveyReference("Collection_2", myInvitation.get("_id").toString());

this.invitations = reference;


But i getting excepton::

05-06 13:55:47.372: W/System.err(3901): java.lang.NullPointerException

05-06 13:55:47.372: W/System.err(3901): at org.pac.mypac.kunvey2.HelloEntity.initReference(

05-06 13:55:47.372: W/System.err(3901): at org.pac.mypac.kunvey2.Main.storeDataOnKinvey(

05-06 13:55:47.372: W/System.err(3901): at org.pac.mypac.kunvey2.Main$myAsyncTask2.doInBackground(

is there something wrong in my code ???

The problem is probably that eve2 doesn't have an _id yet.

Before calling on event, call it on eve2. Then, after eve2 is saved, in the onSuccess callback, call event.initReference(arg0), and then use appdata to save event.

For these to work, both entities must be saved to your backend, which will create a unique _id for them.
Thanx Edward,

Now data is saving on both Collection(Collection_1, Collection_2).

In collection_1 there are one column name "invitations" which hold these data


Now I want get that data back using:

AsyncAppData myevent = mKinveyClient.appData("Collection_1", HelloEntity.class);

myevent.get(new KinveyListCallback() {


public void onSuccess(HelloEntity[] result) {

for (HelloEntity object : result) {

System.out.println("Object_ID :: "+object.getId().toString());



public void onFailure(Throwable error) {}


What is missing in my code.

Login or Signup to post a comment