Iterating through BackendlessCollection

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

Probably I have to update the title of the question.

I iterated through the getData() and log the .getClass() of the objects.
Each of them are plain java.util.Hashmap.

Which means that the problem is in fact Backendless not retrieving the data as custom objects; rather simple Hashmaps. It doesn’t seem right.
Shouldn’t I get back the instances of my Contact class?
That’s what the notation implies and documentation states.

Clearly, I’m missing something pretty obvious here, and will feel very stupid after your answer :slight_smile:

~ Fatih

I saw a reply from Mark for a different question as “Are you using the latest SDK?”.

So, I thought that I should give it a try and re-downloaded the generated code.
Copy-pasted all my handlers into the newly downloaded project.

Now, it’s working as expected.

I have no idea why it happened and why it’s solved.
I didn’t go through the project settings nor altered anything besides the actual code of the handlers.

Anyways, for a future reference: downloading a fresh copy of the SDK and/or generated code might solve many different / seemingly unrelated issues.

~ Fatih