Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving a child referenced via @LazyReference DatastoreDataException #1203

Open
mmihira opened this issue Jul 27, 2022 · 2 comments · May be fixed by #1205
Open

Saving a child referenced via @LazyReference DatastoreDataException #1203

mmihira opened this issue Jul 27, 2022 · 2 comments · May be fixed by #1205
Assignees
Labels

Comments

@mmihira
Copy link

mmihira commented Jul 27, 2022

Describe the bug

Given Parent references a Child via @LazyReference.

Retrieve the parent, then access the child.
Make a modification to only the child and then attempt to save the child.
An error results:

error on save here: com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException: Cloud Datastore can only allocate IDs for Long and Key properties.
Cannot allocate for type: class java.lang.String

Sample
https://github.com/mmihira/spring-datastore-reference-example

The test is below.

    // create parent with child
    Child child = Child.builder().id(UUID.randomUUID().toString()).value("childFoo").build();
    Parent parent = Parent.builder().id(UUID.randomUUID().toString()).value("parentFoo").child(child).build();
    Parent savedParent = parentRepository.save(parent);

    // retrieve the saved parent
    Parent retrievedParent = parentRepository.findById(savedParent.getId()).get();
    assertThat(retrievedParent.getValue()).isEqualTo("parentFoo");
    assertThat(retrievedParent.getChild().getValue()).isEqualTo("childFoo");

    // try saving the child (LazyReference)
    // error on save here: com.google.cloud.spring.data.datastore.core.mapping.DatastoreDataException: Cloud Datastore can only allocate IDs for Long and Key properties.
    // Cannot allocate for type: class java.lang.String
    Child retrievedChild = retrievedParent.getChild();
    retrievedChild.setValue("bar");
    childRepository.save(retrievedChild);
@zhumin8
Copy link
Contributor

zhumin8 commented Jul 28, 2022

Thanks for reporting this, I was able to reproduce this issue and it looks like to me that writing/updating an entity that's lazy referenced is not supported in the code base as of now. We'll look into this bug a bit more.
In the meantime, a less ideal temporary workaround is to do a copy of this retrievedChild and saving this copy instead. Something similar to this for your sample code:

        Child childToSave = Child.builder().id(retrievedChild.getId()).value("updated child").build();
        childRepository.save(childToSave);

@zhumin8 zhumin8 linked a pull request Jul 29, 2022 that will close this issue
@mmihira
Copy link
Author

mmihira commented Aug 10, 2022

Thanks for looking into it.

@alicejli alicejli added the type: bug Something isn't working label Sep 6, 2023
@zhumin8 zhumin8 self-assigned this Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants