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](http://www.kinvey.com/blog/3732/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



http://devcenter.kinvey.com/android/guides/datastore#RelationalData





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



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);

myevents.save(event, new KinveyClientCallback() {

@Override

public void onFailure(Throwable e) {

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

}

@Override

public void onSuccess(MyEntity r) {

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

}

});
Hey,





`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();



event.setUserId("xxx");

event.setPatientIdString("yyyyy");

event.setReportId("R006");



eve2.setObjectId("100001");

eve2.setStatus("new status");



event.initReference(eve2);

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

myevents.save(event, new KinveyClientCallback() {

@Override

public void onFailure(Throwable e) {}



@Override

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(HelloEntity.java:117)



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



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



is there something wrong in my code ???
Hey,



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



Before calling appdata.save 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

{"_collection":"Collection_2","_id":"5369d9b841214b2f7503fef9","_type":"KinveyRef"}



Now I want get that data back using:



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



myevent.get(new KinveyListCallback() {

@Override

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