-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
daa6353
commit 17bf02e
Showing
3 changed files
with
55 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC206.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
18 changes: 18 additions & 0 deletions
18
java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC206.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |