Skip to content

Commit

Permalink
feat: Ajout de la documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hayaofr authored and javathought committed Apr 6, 2023
1 parent daa6353 commit 17bf02e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package fr.greencodeinitiative.java.checks;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Optional;


import org.sonar.check.Priority;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.*;

@Rule(key = "EC206",
Expand Down Expand Up @@ -38,7 +34,7 @@ public class AvoidNPlusOneQueryProblemCheck extends IssuableSubscriptionVisitor

@Override
public List<Tree.Kind> nodesToVisit() {
return Arrays.asList(Tree.Kind.INTERFACE);
return List.of(Tree.Kind.INTERFACE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<p>When using JPA on a relation One to Many, Many to One or Many to Many, you should use a query (Query annotation) with LEFT JOIN FETCH or use attribute path (@EntityGraph annotation) to avoid the generation of N+1 query instead of 1 query </p>
<h2>Noncompliant Code Example</h2>
<pre>
public interface UserRepository extends CrudRepository<User, Long> {

List<User> findAllBy();
}

@Entity
public class User {

@OneToMany
private List<Role> roles;

public List<Role> getRoles() {
return roles;
}

public void setRoles(List<Role> roles) {
this.roles = roles;
}
}

</pre>
<h2>Compliant Solution</h2>
<pre>
<pre>
public interface UserRepository extends CrudRepository<User, Long> {

@Query("SELECT p FROM User p LEFT JOIN FETCH p.roles")
List<User> findWithoutNPlusOne();

@EntityGraph(attributePaths = {"roles"})
List<User> findAll();
}
</pre>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"title": "Avoid to generate N+1 query when using JPA on relations One to Many, Many To One or Many To One",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "10min"
},
"tags": [
"eco-design",
"java",
"performance",
"bug",
"ecocode",
"sql"
],
"defaultSeverity": "Minor"
}

0 comments on commit 17bf02e

Please sign in to comment.