vladmihalcea / hypersistence-optimizer

Hypersistence Optimizer allows you to get the most out of JPA and Hibernate. By scanning your application configuration and mappings, Hypersistence Optimizer can tell you what changes you need to do to speed up your data access layer.
https://vladmihalcea.com/hypersistence-optimizer/
Apache License 2.0
318 stars 43 forks source link

NaturalIdCacheEvent is triggered even when having the @NaturalIdCache in place #245

Open rameshsahydstgdn opened 5 months ago

rameshsahydstgdn commented 5 months ago

Issue Summary:

The Hypersistence Optimizer is suggesting to add the @NaturalIdCache annotation to entities that already have it, specifically when using natural identifiers. This recommendation appears even when the annotation is correctly applied at the entity level.

Steps to Reproduce:

Define an entity in Hibernate with natural identifiers (@NaturalId). Apply the @NaturalIdCache annotation to the entity class. Run the Hypersistence Optimizer or integrate it into a Spring Boot application. Observe the generated NaturalIdCacheEvent warning.

Expected Behavior:

The Hypersistence Optimizer should recognize that @NaturalIdCache is already present on the entity and not suggest adding it again, assuming it's correctly placed.

Actual Behavior:

The optimizer suggests adding @NaturalIdCache even when it is already used in the entity definition. This can lead to confusion and unnecessary recommendations in the optimization report.

Environment:

Hypersistence Optimizer version: 2.9.0, classifier: jakarta Hibernate version: 6.5.2.Final Spring Boot version: 3.3.0 Database: postgresql:16

vladmihalcea commented 5 months ago

Thanks for reporting it. I will try to replicate it according to your indications.

vladmihalcea commented 5 months ago

@rameshsahydstgdn I build this example to replicate the issue.

I assume you added the @NaturalIdCache annotation without activating the second-level cache, and that's why you got the NaturalIdCacheEvent.

If you activate the second-level cache via the spring.jpa.properties.hibernate.cache.region.factory_class property and the Maven dependency (e.g., Ehcache), then the event is no longer triggered, as proven by the test case.

So, in this case, I could change the message of the NaturalIdCache event to make it easier for devs to find out that the cache annotations themselves do nothing in the absence of the second-level cache.

rameshsahydstgdn commented 5 months ago

Thank you for the suggestion! Issue resolved by activating the second-level cache.

vladmihalcea commented 5 months ago

@rameshsahydstgdn I will reopen it in order to investigate a way to change the message based on whether the second-level cache is active or not.