Hardy --
Thanks for checking and getting that issue opened.
It's not that it won't cache it at all. It will. But it ignores the special treatment of (immutable) natural id lookups.
The special treatment is to ignore the last-modified timestamp of the table in the case of a natural id. That way, an insert, update, or delete from the table will not cause the query to be invalidated. As long as the natural key is immutable, the query results can be completely bypassed and the query cache goes directly to level 2 cache to hydrate the object, because the object with an immutable natural key could not have been affected by an insert, update, or delete (as long as it is still valid in the L2 cache). I found this by debugging thru the code. I did not utilize the statistics class to verify, but that is a good idea.
From StandardQueryCache.get()
Code:
Long timestamp = (Long) cacheable.get(0);
if ( !isNaturalKeyLookup && !isUpToDate(spaces, timestamp) ) {
log.debug("cached query results were not up to date");
return null;
}
In this case, if one wanted to verify the behavior (according to what i see in the code), it might go something like this
select, miss (null result NOT cached in this case because of natural id)
select, miss
insert
select, miss (populate cache)
insert a new row
select, hit (hits because update timestamp should be ignored)
as for the one-one nature of my example, I bet you right. But, this was just a simplified example for illustrative purposes. In my real app, I have either two @ManyToOne with @NaturalId, or I have a @ManyToOne and a regular property with @NaturalId (see my reply to Sanne). So the unique key ends up (fk_id, propety) or (fk1_id, fk2_id), and the ManyToOne nature is not broken.
That also tells me maybe the jira issue topic should change, or perhaps i should comment it. In the case of my compound natural id, the simple property still shows up in getNaturalIdentifierProperties(), and the @ManyToOne does not. But this still means the mapping is essentially wrong, and the query cache still mistreats it.
Thanks again for the help.