The problem is in BackrefPropertyAccessor.BackrefGetter.getForInsert(). That method calls StatefulPersistenceContext.getOwnerId(), which returns the ID of the owner, not the collection key.
When a property-ref is used for the collection key, the returned value is (obviously) different from the owner ID.
Currently, this is the only usage of PersistenceContext.getOwnerId(). IMO, this method should be changed to PersistenceContext.getOwnerKey() and the implementation should be changed to return the collection key, not the owner ID.
That would require an SPI change, which can't be done in 4.x. There may be some other very convoluted way to deal with this in 4.x using existing SPIs, but I'm not sure about that.
|