hibernate-orm icon indicating copy to clipboard operation
hibernate-orm copied to clipboard

Hhh-17562 Allow disabling of NaturalIDCaching

Open tg-freigmbh opened this issue 1 year ago • 3 comments

https://hibernate.atlassian.net/browse/HHH-17562 Added a new Option to turn off Hibernates Natural-ID Cache. Motivation is that I ran into multiple bugs and performance problems involving that cache. It seems to me the cache is only usefull if someone uses the strange semantics of Session.byNaturalId to find objects from cache-> and for a lot of usecases you dont need to. Having this Option "breaking" Hibernate functionality is a bit sad, however I would argue there is a huge problem by design if hibernate has to track each object in every usecase because someone might want to use NaturalLoadId in some usecases.

Setting this Option will alter the behavior of Session.byNaturalID a bit -> Objects will be always looked up from the database, so if you change your natural id and didn't flush your entity to the database, you cant find it anymore.

This option allows one to workaround HHH-17556-> no more exceptions because of bad caching. As another argument for this change see included: org.hibernate.orm.test.mapping.naturalid.performance.PerformanceTest disabling the natural Id Resolutions Cache allows the test to complete as expected-> having it enabled will most likely (but not guaranteed) throw an StackoverflowError

Best regards

https://hibernate.atlassian.net/browse/HHH-17652

tg-freigmbh avatar Jan 19 '24 16:01 tg-freigmbh

Love the idea, thanks!

sebersole avatar Jan 19 '24 17:01 sebersole

As another argument for this change see included: org.hibernate.orm.test.mapping.naturalid.performance.PerformanceTest

The reason for the stack overflow in that test is that you have an insane implementation of equals() that tries to recursively walk a linked list with 100k objects in it. The stack overflow has nothing at all to do with caching, and I can fix it by just changing to a non-insane implementation of equals().

For example, with the following change, the test passes:

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Node node)) return false;
            return node.getNextNode() == nextNode
                && Objects.equals(node.getOtherPart(),otherPart);
        }

        @Override
        public int hashCode() {
            return Objects.hash(ident, otherPart);
        }

Motivation is that I ran into multiple bugs and performance problems involving that cache.

I suspect that you're doing other bad things, such things being the actual source of your problems.

gavinking avatar Jan 19 '24 20:01 gavinking

Love the idea, thanks!

@sebersole The idea might be OK, but the other claims made here are highly suspect.

gavinking avatar Jan 19 '24 20:01 gavinking