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

Cascade insert with managed id ... results in update rather than insert statement #1472

Closed
rbygrave opened this issue Aug 15, 2018 · 0 comments
Assignees
Labels
Milestone

Comments

@rbygrave
Copy link
Member

Steps to reproduce

@Entity
public class OtoAtwo {

  @Id
  private String id;

  private String description;

  @OneToOne(orphanRemoval=true, cascade = CascadeType.ALL)
  private OtoAone aone;
@Entity
public class OtoAone {

  @Id
  private String id;
    OtoAtwo b = new OtoAtwo("b1", "b test");
    Ebean.save(b);

    OtoAone a = new OtoAone("a1", "a test");
    b.setAone(a);

    Ebean.save(b);

Cascading save on b ... will first cascade to save the a instance ... but this incorrectly results in a update rather an insert.


The reason is that when cascading it is ultimately detecting if a instance should be inserted or updated and is falling back to using Id value existence check (in DefaultPersister createRequestRecurse() method).

The fix we can employ is to note that the persist is 'normal' in that we are doing update on a bean that is in LOADED state (so update is normal matching the bean state and this isn't a forced update / stateless update).

So if the parent request is a 'normal' persist then we should use the bean state to determine insert or update on the recursive state to instance a. As the bean state is NEW this results in an insert (what we want) rather than the update (this bug).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant