OBPIH-6757 Fix a bug related to 1:1 association between product suppl…#4904
Merged
awalkowiak merged 2 commits intodevelopfrom Oct 23, 2024
Merged
OBPIH-6757 Fix a bug related to 1:1 association between product suppl…#4904awalkowiak merged 2 commits intodevelopfrom
awalkowiak merged 2 commits intodevelopfrom
Conversation
…ier and product package + fix generating a source code
awalkowiak
reviewed
Oct 22, 2024
| defaultProductPackage.quantity = quantity | ||
| productSupplier.addToProductPackages(defaultProductPackage) | ||
| productSupplier.defaultProductPackage = defaultProductPackage | ||
| productSupplier.save() |
Collaborator
There was a problem hiding this comment.
I think it would be worth adding a comment on why it is saved here and another on why the flush is needed two lines below.
Collaborator
There was a problem hiding this comment.
(I know you described it in the description, but feels like it should be in the code)
4a6bbde to
a3b4c0e
Compare
awalkowiak
approved these changes
Oct 22, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…ier and product package + fix generating a source code
✨ Description of Change
Link to GitHub issue or Jira ticket:
Description:
The fix for the main problem was adding a flush in product package save. Why it is needed is due to the fact of our complicated relationships in the product supplier domain - we have both 1:1 (
defaultProductPackage) and 1:N (productPackages) relationships between product supplier and product package.When attempting to save a
productPackagewithout saving theproductSupplierbefore, we would get an error that we try to use a "transient instance must be saved before current operation".On the other hand, if we save the
productSupplierbefore and then save theproductPackageand assign it to theproductSuppliereverything looks great in the debugger - we can see that theproductSupplier.defaultProductPackageis assigned properly as well asproductSupplier.productPackageslist contains the package.The problem was that after the session was flushed and the transaction was commited, the
productSupplier.defaultPackagewas cleared (becamenull), whereas the list (productSupplier.productPackages) stayed untouched.It's due to the queue that Hibernate generated for those operations as the transaction commit - when we call the
save()without flush, the sql logs look like this:so as you can see, we save the product supplier before the product package, so the
default_product_package_idmust benullat this point.As for the 1:N bidirectional association, it is enough to store the foreign key in the product package table, so in the
product_supplier_idproperty which is NOT null at this point (hence the list is not cleared, whereas the 1:1 is cleared).When we call
save(flush: true)though, the logs look like this:so the product supplier is updated with the existing product package, that has just been persisted - this didn't take place in the first version and this is why the
productSupplier.defaultProductPackagewasnullafter the transaction was commited.📷 Screenshots & Recordings (optional)