green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[WEB][42108] Best practice JPA repositories use #83

Open JulienBertrand opened 1 year ago

JulienBertrand commented 1 year ago

\newpage

Best practice JPA repositories use

Platform

OS OS version Langage
{Windows} {} {JAVA}

Main caracteristics

ID Title Category Sub-category
CRJVM007-42108 Use Pageable on search methods of JPA {Spring} {JPA}

Severity / Remediation Cost

Severity Remediation Cost
Major Major

Rule short description

Rule complete description

Text

In many cases we only need to retrieve or display a subset of data and not all the data. To avoid unneeded data retrieval, it's better to make paginated queries to the database. Spring data has a Pageable feature implement by default by the JpaRepository which extends the PagingAndSortingRepository. All calls to search methods should ase a Pageable argument to specify the size and the number of the current page, this must be done on find* methods on a JPA repository.

JAVA Example

// Uncompliant code:

List<User> users = userRepository.findByFirstName(firstname);

// Compliant code:

Page<User> users = userRepository.findByFirstName(firstname, pageable);

Implementation principle

jhertout commented 2 months ago

Interesting but difficult to be sure that the rule is energy efficient (no documentation or something else to justify). May be a rule to measure in the next hackaton.

dedece35 commented 1 month ago

ok with you @jhertout