Skip to content

Conversation

@NassimAMGHAR
Copy link

Adding a new rule to check GRC3 : N+1 problem when calling a nested Object in an entity that causes multiples repository calls ->

#36

@NassimAMGHAR NassimAMGHAR changed the title Grc3 feature n+1 issue GCI604 V.E.R.T Grc3 feature n+1 issue May 21, 2025
@jbureau92 jbureau92 self-assigned this May 21, 2025
public List<Author> smellGetAllAuthors() {
List<Author> authors = authorRepository.findAll();
for (Author author : authors) {
List<Book> books = author.getBooks(); // // Noncompliant {{ Evitez le N+1 : utilisez un fetch join ou une récupération eager. }}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More precisely, it's the call to a method on getBooks() (for example, .size() or .get(5)) that triggers the initialization of the list, causing SQL queries to be executed — and that's where the problem occurs.
Calling getBooks() alone simply returns the proxy; it does not trigger any query by itself.
However, I believe that calling getBooks() is enough to justify raising the rule violation: why retrieve the proxy if nothing is done with it afterward?
That’s why we should ensure the rule’s description clearly explains why this is an issue.

✅ Agreed: no need to simulate a method call; just the getBooks() access is enough to be considered problematic.
✅ The comment should be written in English.
✅ Remove the double //.
✅ Add a proper rule description.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agree, changed the comment to match the specification

@NassimAMGHAR NassimAMGHAR force-pushed the GRC3-feature-N+1-issue branch from 5d76fdd to 0b9e962 Compare May 26, 2025 18:47
@github-actions
Copy link

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Jun 26, 2025
@Rule(key = "GCI604")
public class AvoidNPlusOneProblemInJPAEntitiesCheck extends IssuableSubscriptionVisitor {

protected static final String RULE_MESSAGE = " Detection of the \"N+1 problem\" on Spring Data JPA repositories ";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please delete de first space in the message

}
}

// Cas d'un foreach sur une variable issue de findAll()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please us english in comments in all files

if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) tree;

// Check if the call is something like post.getAuthor() or post.getAuthor().getName()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comments contains example and not the algorithms description

CheckVerifier.newVerifier()
.onFile("src/test/files/AvoidNPlusOneProblemInJPAEntitiesCheckIssue.java")
.withCheck(new AvoidNPlusOneProblemInJPAEntitiesCheck())
.withClassPath(FilesUtils.getClassPath("target/test-jars"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need this method call "withClassPath" ?

}


public class Author {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see JPA annotations to link Author and Book objects, like the usage of @entity, @id, @onetomany, ... like described here https://www.baeldung.com/spring-hibernate-n1-problem
sorry, but I don't understand how it works without JPA annotations ...

@github-actions
Copy link

This PR has been automatically marked as stale because it has no activity for 30 days.
Please add a comment if you want to keep the issue open. Thank you for your contributions!

@github-actions github-actions bot added the stale label Aug 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants