Start a new topic

How to access the referenced fields?

i have two collections



















public class Question extends GenericJson

{

@Key

String qid;

@‌Key

String question;

}

public class Response extends LinkedGenericJson

{

@Key

String uid;

@Key

KinveyReference questionId;

@‌Key

String answer;

}

































i am successfully saved and fetched

I want to access the the 'Question' collection fields from the Response class object when it is fetched from the database..

1 person has this question

Ramo,


You should be able to do this simply by invoking appdata.get as outlined in the relationalData - Fetching part of the java guide.   


http://devcenter.kinvey.com/java/guides/datastore#relational-data-fetching


Thanks,

ok, i am having trouble fetching the object. Let's say I have two collections, CollectionA and CollectionB, CollectionA has a kinvet reference to CollectionB. Data is successfully stored on the database, and when I retrieve it, I get the correct _id associated with CollectionA and the reference in CollectionB like so:



But... when I try to do CollectionA.getMyObject().getResolvedObject, it will return null


Note that this is an ArrayList<KinveyReference> 


Can anyone shed light on this?


Thanks



Thanks...Edward i found the answer.

i was referencing different field other than _id of question....
i retrived it using





Query q = new Query();

q.equals("u_id",Splash.mKinveyClient.user().getId());

Splash.mKinveyClient.appData("EmployeeResponse", UserResponseStructure.class).get(q, new String[]{"questionId"},new KinveyListCallback(){





and used



QuestionStructure Q=questionId.getTypedObject(QuestionStructure.class);



to get Question object but i am getting, Error as :

05-19 11:59:07.641: I/error name(1088): java.lang.Object cannot be cast to com.google.api.client.util.ArrayMap

If both entities are successfully saved, and the reference has been created, than you can resolve it using appdata.



Try something like this (I am assuming the `Response` collection is called Responses):





Query q = new Query();

myClient.appData("Responses", Response.class).get(q, new String["questionId"], new KinveyListCallback(){ ... });





Note there are overloaded variations of the `appdata.get` and `appdata.getEntity` methods, which all take a String[] as a second argument. Use that String array to pass the field names of the KinveyReferences you want to resolve, in this case it is `questionId`.



Now, within the callback, you will have an array of `Response` objects. One each one you can call either `getTypedObject(Question.class)`, or just `getResolvedObject()` to get the GenericJson class for the Question object. Take a look at the KinveyReference documentation, it might help explain this: http://devcenter.kinvey.com/android/reference/api/java/reference/com/kinvey/java/model/KinveyReference.html





Let me know if that works, and if you have any more questions.
Login or Signup to post a comment