|
| 1 | +/* |
| 2 | + * creedengo - Java language - Provides rules to reduce the environmental footprint of your Java programs |
| 3 | + * Copyright © 2024 Green Code Initiative (https://green-code-initiative.org/) |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package org.greencodeinitiative.creedengo.java.checks; |
| 19 | + |
| 20 | +import org.springframework.beans.factory.annotation.Autowired; |
| 21 | +import org.springframework.data.jpa.repository.JpaRepository; |
| 22 | + |
| 23 | +import java.util.*; |
| 24 | + |
| 25 | +public class AvoidNPlusOneProblemInJPAEntitiesCheck { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + private AuthorRepository authorRepository; |
| 29 | + |
| 30 | + public List<Author> smellGetAllAuthors() { |
| 31 | + List<Author> authors = authorRepository.findAll(); |
| 32 | + for (Author author : authors) { |
| 33 | + List<Book> books = author.getBooks(); // Cela peut déclencher une requête SQL pour chaque auteur |
| 34 | + } |
| 35 | + return authors; |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + public class Author { |
| 40 | + |
| 41 | + private Long id; |
| 42 | + private String name; |
| 43 | + |
| 44 | + private List<Book> books; |
| 45 | + |
| 46 | + |
| 47 | + public Long getId() { |
| 48 | + return id; |
| 49 | + } |
| 50 | + |
| 51 | + public void setId(Long id) { |
| 52 | + this.id = id; |
| 53 | + } |
| 54 | + |
| 55 | + public String getName() { |
| 56 | + return name; |
| 57 | + } |
| 58 | + |
| 59 | + public void setName(String name) { |
| 60 | + this.name = name; |
| 61 | + } |
| 62 | + |
| 63 | + public List<Book> getBooks() { |
| 64 | + return books; |
| 65 | + } |
| 66 | + |
| 67 | + public void setBooks(List<Book> books) { |
| 68 | + this.books = books; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public class Book { |
| 74 | + |
| 75 | + private Long id; |
| 76 | + private String title; |
| 77 | + |
| 78 | + private Author author; |
| 79 | + |
| 80 | + public Long getId() { |
| 81 | + return id; |
| 82 | + } |
| 83 | + |
| 84 | + public void setId(Long id) { |
| 85 | + this.id = id; |
| 86 | + } |
| 87 | + |
| 88 | + public String getTitle() { |
| 89 | + return title; |
| 90 | + } |
| 91 | + |
| 92 | + public void setTitle(String title) { |
| 93 | + this.title = title; |
| 94 | + } |
| 95 | + |
| 96 | + public Author getAuthor() { |
| 97 | + return author; |
| 98 | + } |
| 99 | + |
| 100 | + public void setAuthor(Author author) { |
| 101 | + this.author = author; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + public interface AuthorRepository extends JpaRepository<Author, Long> { |
| 106 | + |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments