blaze-persistence icon indicating copy to clipboard operation
blaze-persistence copied to clipboard

Saving view and querying backing entity immediately not reflecting "mappedBy" property

Open ccoffey1 opened this issue 2 years ago • 0 comments

Description

See Zulip discussion here

When saving an entity view for an entity that has a one-to-one relationship, the "mappedBy" property of the related entity is null if the related entity is queried immediately (before the transaction is committed).

Example:

Entity:

@Entity
public class Test {

   @OneToOne(mappedBy = "child")
   private Test parent;

   @OneToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "child_id", referencedColumnName = "id")
   private Test child;

   // getters n setters
}

Updatable view:

@EntityView(Test.class)
@UpdatableEntityView(strategy = FlushStrategy.ENTITY)
public interface TestUpdateView {
   EntityViewManager evm();

   default Long getChildId() { return getChild().getId(); }
   default void setChildId(Long id) { setChild(evm().getReference(ChildView.class, id)); }

   @JsonIgnore
   ChildView getChild();
   @JsonIgnore
   void setChild(ChildView child);
}

@EntityView(Test.class)
public interface ChildView {
   @IdMapping
   public Long getId();
}

When the above TestUpdateView is saved with an ID set via setChildId() and then that child is queried as an entity within the same transaction, that child's parent property will be null. Once the transaction is committed, parent will have a value as expected.

Expected behavior

When using the ENTITY Flush Strategy, it is expected that parent in the above example would have a value even before the transaction is committed.

Actual behavior

parent in the above example is null until the transaction is committed.

Steps to reproduce

  1. Create a table or two tables that have a one-to-one bidirectional relationship.
  2. Setup and Entity/Entity View for the table(s).
  3. Ensuring two records exist in either the same table or one in each table, save an updatable entity view that links them together. (In the example above, by setting childId = to the other record's ID)
  4. Prior to the transaction being committed, query for the entity (not entity view) by the ID you just saved. It will have a null value for the one-to-one property.
  5. Commit the transaction and re-query the entity. It will now have a value for this property.

Environment

Version: 1.6.7 JPA-Provider: Hibernate 5.6.11.Final (Spring Data JPA 2.7.4) DBMS: PostgreSQL 13.3 Application Server: Spring Boot 2.7.4

ccoffey1 avatar Jan 24 '23 20:01 ccoffey1