False positive HibernateException: Found shared references to a collection with 6.2.2

Hi,
Trying to update hibernate 6.2.2 and getting Found shared references to a collection
It is a bit hard to explain exactly as system is quite old and big and hard exactly to find everything what it is doing during an action that causes the issue, but I think I was able to isolate issue in the PR.

So I have the following setup:
Product → one-to-one->Sku-(embeds)-PriceListSkuImpl->one-to-many->SkuPriceData
|
one-to-many
|
CategoryProductXref->many-to-one->Category

At some point I’m “cloning” a product(given the error I checked a number of times that I don’t re-assign or “clone” any collection) which in test-case I mimic by creating a new product. Then do refresh to both original and clone and that’s it.
In test I also read category, and some other product, and initialize collections by accessing them, which seems is required to reproduce the issue.
So once the final flush is happening I’m getting the Found shared references error for SkuImpl.priceListSku.priceDataList
And the interesting thing that error(if you do debug) will be related to some other product/sku that didn’t participate in the cloning process, but just belong to the same category that original product does.

While debigging I think there is some issue with handling cycles in the entity graph, so seems it does:
Product->sku->priceDataList
then it does
Product->category_xref->category->all-product-in-category->category_xref->product->sku->priceDatalist
and fails…

I fixed the issue in my project just removing cascade.REFRESH
While I think it is required to review refresh cascades as they are defined on both sides or relationships(not sure why but this is very old code), I think this is not an error and no exception should be thrown.
Please correct me if I’m wrong and mapping is incorrect.
This setup was working for ages since I think hibernate 3…

here it is PR that demonstrates issue

Thanks a lot for working on a reproducer for this. Can you please create an issue in the issue tracker(https://hibernate.atlassian.net) and attach that reproducer there?

thanks for your answer
https://hibernate.atlassian.net/browse/HHH-16665

I seem to have a similar issue, with Hibernate 6.3.1.Final.
Works fine with 6.2.13.Final.