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
306 stars 43 forks source link

Spring JPA existsBy query flagged as PaginationWithoutOrderByEvent #161

Closed BartVansegbroeck closed 2 years ago

BartVansegbroeck commented 2 years ago

Method in our JPA repository boolean existsByMerchantIdAndId(MerchantId MerchantId, CId cId)

is translated by hibernate to Hibernate: select c0_."id" as col_0_0_ from "c_v" c0_ where c0_."merchant_id"=? and c0_."id"=? limit ?

And flagged by Hypersistence as unsorted paging event select generatedAlias0.id from Customer as generatedAlias0 where ( generatedAlias0.merchantId=:param0 ) and ( generatedAlias0.id=:param1 )

Given that this is an exists query (hence the LIMIT 1), order is irrelevant and I would think this is a false positive.

vladmihalcea commented 2 years ago

Yes, indeed, this is a false positive.

Anyway, the usage of LIMIT is strange as there cannot be more than one result. Ideally, Spring would have not used LIMIT and just throw an exception when there are multiple results being returned by the SQL query.

In the meanwhile, you can use an EventFilter for this type of event so that it does not bother you until I find a good way to fix it.

The solution will scan the query AST and figure out whether the filtering is done by the entity id or natural id.

vladmihalcea commented 2 years ago

Fixed.