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

NEW: MergeBeans function instead of updating existing bean from json #88

Merged
merged 14 commits into from
May 12, 2023

Conversation

nPraml
Copy link

@nPraml nPraml commented May 8, 2023

When updating complex object graphs with the existing DB.toJson(existingBean, json) this might fail, if you have cyclic references.

Explanation
Ebean uses streaming when reading/writing jsons.
If you have a rather complex object graph with cycles, you may have problems when deserializing them.

As example take the class "User"

class User {
   @id int id;
   String name;
   @ManyToOne @WhoCreated User whoCreated;
   @ManyToOne @WhoCreated User whoModified;
} 

So your initial user could look like

User root = new User();
root.name = "Root"
root.whoCreated=root; // bootstrap ourself (makes cycle)
root.whoModified=root;
DB.save(root);

If you serialize this, your JSON will look like

{
  "id" : 1,
  "name" : "Root",
  "whoCreated": { "id" : 1 },
  "whoModified": { "id" : 1 },
}

Serializion works fine, it stores just references. When it comes to deserialization, the following happens:

bean1 = desc.createEntityBeanForJson();
bean1.id = 1;
bean1.name = "Root"
// first recursion
bean2 = desc.createEntityBeanForJson();
bean2.id = 1;
contextPutIfAbsent (first put)
bean1.whoCreated = bean2
// second recursion
bean3 = desc.createEntityBeanForJson();
bean3.id = 1;
contextPutIfAbsent (second put) - we will find "bean2" as contextBean
desc.merge(bean3, bean2) // copy all changed fields from bean3 to bean2 (there are none)
bean1.whoCreated = bean2

The more complex the json structure is, the more problems will appear.

This PR reverts the workarounds in ebean-orm#3000 (where we must buffer the json)
and mainly reverts ebean-orm#2617
(it will keep the API as deprecated)

It is recommeded now to do deserialize the JSON bean first and use Database.mergeBeans(jsonBean, targetBean)

@nPraml nPraml requested a review from rPraml May 8, 2023 06:25
@rPraml rPraml changed the title FIX: update json with references NEW: MergeBeans function instead of updating existing bean from json May 12, 2023
@rPraml rPraml merged commit 73ad2bd into master May 12, 2023
rPraml added a commit that referenced this pull request Aug 3, 2023
rPraml added a commit that referenced this pull request Aug 3, 2023
rPraml added a commit that referenced this pull request Aug 3, 2023
rPraml added a commit that referenced this pull request Aug 8, 2023
rPraml added a commit that referenced this pull request Aug 8, 2023
rPraml added a commit that referenced this pull request Aug 9, 2023
rPraml added a commit that referenced this pull request Aug 9, 2023
rPraml added a commit that referenced this pull request Aug 9, 2023
rPraml added a commit that referenced this pull request Aug 9, 2023
rPraml added a commit that referenced this pull request Aug 9, 2023
rPraml pushed a commit that referenced this pull request Aug 9, 2023
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
rPraml pushed a commit that referenced this pull request Aug 9, 2023
…88)

Co-authored-by: Roland Praml <[email protected]>

FIX: Merge of discriminator

FIX: Update reference (#89)
@rPraml rPraml deleted the fix-update-json branch August 10, 2023 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants