0
votes

I am trying to retrieve an entity immediately after it was saved. When debugging, I insert the entity, and check entities in google cloud console, I see it was created.

Key key = datastore.put(fullEntity)

After that, I continue with getting the entity with

datastore.get(key)

, but nothing is returned. How do I retrieve the saved entity within one request?

I've read this question Missing entities after insertion in Google Cloud DataStore but I am only saving 1 entity, not tens of thousands like in that question

I am using Java 11 and google datastore (com.google.cloud.datastore. package)*

edit: added code how entity was created

public Key create.... {
    // creating the entity inside a method
    Transaction txn = this.datastore.newTransaction();

    this.datastore = DatastoreOptions.getDefaultInstance().getService();
    Builder<IncompleteKey> builder = newBuilder(entitykey);

    setLongOrNull(builder, "price", purchase.getPrice());
    setTimestampOrNull(builder, "validFrom", of(purchase.getValidFrom()));
    setStringOrNull(builder, "invoiceNumber", purchase.getInvoiceNumber());
    setBooleanOrNull(builder, "paidByCard", purchase.getPaidByCard());

    newPurchase = entityToObject(this.datastore.put(builder.build()));
    if (newPurchase != null && purchase.getItems() != null && purchase.getItems().size() > 0) {
        for (Item item : purchase.getItems()) {
            newPurchase.getItems().add(this.itemDao.save(item, newPurchase));
        }
    }
    txn.commit();
    return newPurchase.getKey();
}

after that, I am trying to retrieve the created entity

Key key = create(...);
Entity e = datastore.get(key)
2
Could you add the code you use to create the entity and the declaration of the variable datastore? I believe you issue has to do with your get request, but I need to confirm it. - Ajordat
@Ajordat I added the code - hocikto
I'm afraid this code is quite different than the originally provided sample and I now have doubts about the root cause. Could you please add the lines where you retrieve the added entity? - Ajordat
@Ajordat what about now? I added more code. it's just a call to a method which returns the key that I am using for retrieval of the newly created entity. There are two classes, if it helps... service calls dao to create and then in service I try to retrieve the created entity - hocikto

2 Answers

1
votes

Since you're creating a transaction for this operation, the entity put should happen inside the transaction:

txn.put(builder.builder());

Also, the operations inside the loop where you add the purchase.getItems() to the newPurchase object should also be done in the context of the same transaction.

Let me know if this resolves the issue.

Cheers!

1
votes

I believe that there are a few issues with your code, but since we are unable to see the logic behind many of your methods, here comes my guess.

First of all, as you can see on the documentation, it's possible to save and retrieve an entity on the same code, so this is not a problem.

It seems like you are using a transaction which is right to perform multiple operations in a single action, but it doesn't seem like you are using it properly. This is because you only instantiate it and close it, but you don't put any operation on it. Furthermore, you are using this.datastore to save to the database, which completely neglects the transaction.

So you either save the object when it has all of its items already added or you create a transaction to save all the entities at once.

And I believe you should use the entityKey in order to fetch the added purchase afterwards, but don't mix it.

Also you are creating the Transaction object from this.datastore before instantiating the latter, but I assume this is a copy-paste error.