Currently Hibernate's second-level cache doesn't support @OneToOne. The issue here is the entity which is not fetched from cache is mapBy field and currently Hibernate only supports collection cache as the only non-id cache (see https://docs.jboss.org/hibernate/orm/5.4/userguide/html_single/Hibernate_User_Guide.html#caching for details). In the case of @OneToOne, the mapBy field is not of collection but of scalar so it is not within the current l2c scope.
see https://hibernate.zulipchat.com/#narrow/stream/132096-hibernate-user/topic/2nd.20lvl.20cache for further details
The assemble method of the OneToOneType has the following code:
I’m not sure what to make of the comment here, but when oid is null this method assumes that the id of the related one-to-one object is the same as it’s parent. This results in a subsequent cache miss and then a database query to verify the original null identifier. I believe this should be changed to:
I don’t know if this address all of the issues with the one-to-one type but it greatly reduces cache misses when assembling the owner of the one-to-one.
It seems that I’ve overlooked the fact that the disassemble method currently always returns null. I suspect that changes are needed there as well so that disassembled instances can be assembled correctly.