blaze-persistence
blaze-persistence copied to clipboard
Saving view and querying backing entity immediately not reflecting "mappedBy" property
Description
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
- Create a table or two tables that have a one-to-one bidirectional relationship.
- Setup and Entity/Entity View for the table(s).
- 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) - Prior to the transaction being committed, query for the entity (not entity view) by the ID you just saved. It will have a
nullvalue for the one-to-one property. - 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