Hi,
I can’t believe that I’m asking such a simple question but I had to.
How can I iterate through or fetch a single object from BackendlessCollection?
It’s for a custom event handler in Java Business Logic and all calls are synchronous.
Here is the first thing I tried:
Contact contact = Backendless.Persistence.of( Contact.class ).find( query ).getData().get( 0 );
And Java threw an exception of :
“SEVERE: java.util.HashMap cannot be cast to com.mysuperduperapp.models.Contact”
Then:
BackendlessCollection<Contact> results = Backendless.Persistence.of( Contact.class ).find( query );
List<Contact> data = results.getData();
Contact contact = data.get( 0 );
Same error, again.
Then I tried to iterate through the list to no avail.
After that, I tried a Stackoverflow answer as putting the following line at the beginning of the handler:
Backendless.Persistence.mapTableToClass( "Contact", Contact.class );
Same error.
Then I tried to fetch it by hard-coding the object id as:
Contact contact = Backendless.Persistence.of( Contact.class ).findById( "HARD-CODED-ID-OF-OBJECT" );
At least a different error, this time:
“SEVERE: Unable to adapt response to Contact”
What am I doing wrong, it can’t be a bug or fault of the backend, right?
Simply because, I can see the object has been fetched successfully when I log the raw getData().
Thanks,
~ Fatih