Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Load entity by id is failing inconsistently #512

@nitinch23

Description

@nitinch23

I am trying to load an entity from datastore by :
objectifyFactory.ofy().load().type(clazz).id(id).now();

However, this returns a null, even though the entity is present in the datastore. In another function, this runs just fine. I don't understand what might be causing this inconsistent behaviour.

Details:

Function 1:

public String addEntity(VaultEntry vaultEntry, Class<? extends VaultEntry> clazz) {
    return doWorkWithObjectify(() -> objectifyFactory.ofy().transact(() -> {

      // Check if the entity already exists
      VaultEntry alreadyExistingEntry =
          objectifyFactory.ofy().load().type(clazz).id(vaultEntry.getId()).now();  
     // <--- This returns the existing entity just fine. No issues here.

Function 2:

public VaultEntry getEntityById(String id, Class<? extends VaultEntry> clazz) {
    return doWorkWithObjectify(() -> {
      VaultEntry alreadyExistingEntry = objectifyFactory.ofy().load().type(clazz).id(id).now();
    // <---- This returns a null.

My DAO looks like this:
VaultEntry is an interface. SimpleVaultEntry is an implementation of this interface, with fields "id" which is annotated with @Id and "referenceKey" which is annotated with @Index. SimpleVaultEntry is tagged with @Entity.

I am not using the ObjectifyService but directly working with ObjectifyFactory object. This is because I have multiple ObjectifyFactory objects connecting with different datastores at the same time.

In the above 2 functions, I have verified that objectifyFactory object is same in both the functions, however objectifyFactory.ofy() object is different (which is expected since Objectify object is local to a thread).

This is my doWorkWithObjectify function:

  private <R> R doWorkWithObjectify(Work<R> work) {
    try (Closeable session = objectifyFactory.open()) {
      return work.run();
    } catch (IOException e) {
      throw new RuntimeException("Error closing Objectify session", e);
    }
  }

What might be causing this issue? What other info can I provide to help debug this further?
Thanks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions