Problem updating OrderRequestItem
The OrderRequestItemServiceImpl#update
method takes the incoming data and directly applies it to target
:
public OrderRequestItem update(OrderRequestItem input, OrderRequestItem target) {
LOG.debug("Update OrderRequestItem. Input data {}", input);
// save order request
input.setOrderRequest(target.getOrderRequest());
target.apply(input);
var saved = repository.save(target);
return _lazyLoad(saved);
}
While this works for basic fields, in this case the @PreUpdate/@PrePersist
in OrderRequestItem
does some validation against this.inventory
and this.withdrawnInventory
. But withdrawnInventory
is not loaded from the database, it is simply applied to target
.
When { ..., "withdrawnInventory": { "id": 127121 } }
is received, this object has no accession
!
In the #update(source, target)
method we should get source.withdrawnInventory
and source.inventory
from the database before applying them to target
.
The current update()
fails with NPE
when we try to access this.inventory.accession
and this.withdrawnInventory.accession
.
Stacktrace
2022-06-29 15:06:27 qtp542060780-12 WARN ApiExceptionHandler:238 - Wow! Such! Exception!
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:571)
at org.gringlobal.api.v1.CRUDController.update(CRUDController.java:147) ~[classes/:?]
at org.gringlobal.api.v1.FilteredCRUDController.update(FilteredCRUDController.java:140) ~[classes/:?]
Caused by: javax.persistence.RollbackException: Error while committing the transaction
Caused by: java.lang.NullPointerException
at org.gringlobal.model.OrderRequestItem.checkOrderRequestItem(OrderRequestItem.java:131) ~[classes/:?]