Subsequent calls with the same parameters does not trigger some entity functions like @PostLoad

One the first fetch call, all the @PostLoad functions were triggered, but on doing the same fetch call again, even though it shows there is sql query fired to the DB to fetch the entities, it doesn’t trigger the @PostLoad function. (e.g Reloading a browser page with the same search parameters).

Is there some level of caching that is preventing this trigger?

Hibernate Search relies entirely on Hibernate ORM for entity loading, and since the last version (6.0.0.Beta8) it always uses a query under the hood to load entities.

There is some level of caching, but it’s disabled by default. See here.

Now that I come to think of it, it’s possible that some changes in the latest version caused a bug, and that the second level cache is updated and looked up even when cache lookup disabled, due to the implicit cache handling in Hibernate ORM queries… Are you using 6.0.0.Beta8?

I am using 6.0.0.Beta7 as of now, but also tried on 6.0.0.Beta8, got the same behaviour

Ok, I just checked and it seems normal. There’s apparently always been a second level of cache lookup/update when loading search hits, at least in Hibernate Search 6, and probably in Hibernate Search 5 too.

I created HSEARCH-3952 to make this behavior configurable. In the meantime, calling Session.setCacheMode before you run a search query should allow you to get rid of the updates to the second level cache if you don’t want them.

1 Like

Thanks @yrodiere, I will try with that.

@yrodiere So I tried something like

Session session = entityManager.unwrap(Session.class);
session.setCacheMode(CacheMode.IGNORE);
searchResult = queryOptionsStep
                        .fetch(Math.max(pageable.getPageNumber() - 1, 0) * pageable.getPageSize(),
                                pageable.getPageSize());

But it doesn’t seem to work. Am I doing something wrong?

This looks good, it should prevent any sort of second-level cache update.

Maybe the entities are already in your session cache or second-level cache for some other reason? To be absolutely certain, you can launch your application in debug mode, put a breakpoint in implementations of CachedDomainDataAccess#putFromLoad and check who triggered the calls.

This seems to work:

Session session = entityManager.unwrap(Session.class);
                if (Objects.nonNull(session))
                    session.clear();
                searchResult = queryOptionsStep
                        .fetch(Math.max(pageable.getPageNumber() - 1, 0) * pageable.getPageSize(),
                                pageable.getPageSize());

Ok, then your problem was completely unrelated to what I was talking about: you just happen to already have the entities in your session before you run the query, so Hibernate ORM does not have to load them.

If this happens when you refresh a web page, it means you cache the session somewhere between user requests. Be aware that an open, used session maintains a connection to the database, so it’s generally not a good idea to cache them. You should consider closing the session after each HTTP request and re-opening another one in the next request.

We have resolved the issue, by fixing our entityManager configuration. Thanks.