{"id":344924,"date":"2023-02-03T15:01:50","date_gmt":"2023-02-03T15:01:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=344924"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=344924","title":{"rendered":"<span>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1 \u0438 \u043a\u0430\u043a \u0435\u0451 \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e EntityGraph<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 N+1 (\u0438\u043b\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0435\u0435 1+N?) \u0438 \u043a\u0430\u043a \u0435\u0435 \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f EntityGraph.<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0443 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0442\u043e\u0436\u0435 \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 hibernate \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u043d\u0430\u0447\u0430\u043b\u0435 <strong>\u043e\u0434\u0438\u043d (1)<\/strong> <strong>\u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0443\u044e \u043d\u0430\u0441 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c <strong>N<\/strong> <strong>\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u0414\u0430\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437-\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043a \u043d\u0435\u0439.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">&lt;dependencies> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId> &lt;\/dependency> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-web&lt;\/artifactId> &lt;\/dependency> &lt;dependency> &lt;groupId>org.postgresql&lt;\/groupId> &lt;artifactId>postgresql&lt;\/artifactId> &lt;scope>runtime&lt;\/scope> &lt;\/dependency> &lt;dependency> &lt;groupId>org.projectlombok&lt;\/groupId> &lt;artifactId>lombok&lt;\/artifactId> &lt;optional>true&lt;\/optional> &lt;\/dependency> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-test&lt;\/artifactId> &lt;scope>test&lt;\/scope> &lt;\/dependency> &lt;dependency> &lt;groupId>com.github.javafaker&lt;\/groupId> &lt;artifactId>javafaker&lt;\/artifactId> &lt;version>1.0.2&lt;\/version> &lt;\/dependency> &lt;\/dependencies><\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0432\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 Client \u0438 EmailAddress.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/d70\/8d8\/1f4\/d708d81f4d09c6b9e1ac6e380b03b612.png\" width=\"994\" height=\"327\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d70\/8d8\/1f4\/d708d81f4d09c6b9e1ac6e380b03b612.png\"\/><figcaption><\/figcaption><\/figure>\n<pre><code class=\"java\">@Data @NoArgsConstructor @Entity @Table(name = \"client\") public class Client {     @Id     @Column(name = \"id\")     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      @Column(name = \"full_name\")     private String fullName;      @Column(name = \"mobile_number\")     private String mobileNumber;      @OneToMany(cascade = CascadeType.ALL, mappedBy = \"client\")     private List&lt;EmailAddress> emailAddresses;      public Client(String fullName, String mobileNumber, List&lt;EmailAddress> emailAddresses) {         this.fullName = fullName;         this.mobileNumber = mobileNumber;         this.emailAddresses = emailAddresses;     } }<\/code><\/pre>\n<pre><code class=\"java\">@Entity @Table(name = \"email_address\") @Data @NoArgsConstructor public class EmailAddress {     @Id     @Column(name = \"id\")     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      @Column(name = \"email\")     private String email;      @JsonIgnore     @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)     @JoinColumn(name = \"client_id\", referencedColumnName = \"id\")     private Client client;      public EmailAddress(String email) {         this.email = email;     } }<\/code><\/pre>\n<p>\u0421\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 Client \u0438 EmailAddress <code><a class=\"mention\" href=\"\/users\/OneToMany\">@OneToMany<\/a><\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e email \u0430\u0434\u0440\u0435\u0441\u043e\u0432. <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u043a\u0436\u0435 ClientRepository.<\/p>\n<pre><code class=\"java\">public interface ClientRepository extends JpaRepository&lt;Client, Long> { }<\/code><\/pre>\n<p>\u0412 application.properties \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c sql \u043a\u043e\u043c\u0430\u043d\u0434\u044b.<\/p>\n<pre><code class=\"java\">spring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0412\u0430\u0448\u0435\u0439 \u0411\u0414 spring.datasource.username=\u0412\u0430\u0448\u0435 \u0438\u043c\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a postgres spring.datasource.password=\u0412\u0430\u0448 \u043f\u0430\u0440\u043e\u043b\u044c  spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.database=postgresql  spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClientService, \u0433\u0434\u0435 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043d\u0430\u0448\u0443 \u0431\u0430\u0437\u0443. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c 2000 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u0443\u0441\u0442\u044c \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e \u0434\u0432\u0430 email \u0430\u0434\u0440\u0435\u0441\u0430.<\/p>\n<pre><code class=\"java\">@Service public class ClientService {     private final ClientRepository clientRepository;      @Autowired     public ClientService(ClientRepository clientRepository) {         this.clientRepository = clientRepository;     }      public void generateDB(){         List&lt;Client> clients = create2000Clients();         for (int i = 0; i &lt; clients.size(); i++) {             clientRepository.save(clients.get(i));         }     }       public List&lt;Client> create2000Clients() {         List&lt;Client> clients = new ArrayList&lt;>();         Faker faker = new Faker();         for (int i = 0; i &lt; 2_000; i++) {             String firstName = faker.name().firstName();             String lastName = faker.name().lastName();             String sufixTel = String.valueOf(i);             String telephone = \"+375290000000\";              List&lt;EmailAddress>emailAddresses= Arrays.asList(                     new EmailAddress((firstName + lastName).toLowerCase() + \"1\" + i + \"@gmail.com\"),                     new EmailAddress((firstName + lastName).toLowerCase() + \"2\" + i + \"@gmail.com\"));              telephone = telephone.substring(0, telephone.length()-sufixTel.length()) + sufixTel;             Client client = new Client(                     firstName + \" \" + lastName,                     telephone,                     emailAddresses             );              for (EmailAddress emailAddress:emailAddresses) {                 emailAddress.setClient(client);             }              clients.add(client);         }         return clients;     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c ClientController, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/api\/v1\/client\") public class ClientController {      private final ClientService clientService;     private final ClientRepository clientRepository;     @Autowired     public ClientController(ClientService clientService, ClientRepository clientRepository) {         this.clientService = clientService;         this.clientRepository = clientRepository;     }      @ResponseStatus(HttpStatus.OK)     @GetMapping(\"\/fillDB\")     public String fillDataBase() {         clientService.generateDB();         return \"Amount clients: \" + clientRepository.count();     }  }<\/code><\/pre>\n<p>\u0427\u0435\u0440\u0435\u0437 postman \u0441\u0434\u0435\u043b\u0430\u0435\u043c get \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/client\/fillDB \u043d\u0430\u0448\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u0430 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c\u0441\u044f.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/26b\/700\/a1e\/26b700a1ed5f269c09fffdf434a02c67.png\" width=\"1503\" height=\"570\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/26b\/700\/a1e\/26b700a1ed5f269c09fffdf434a02c67.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c ClientRepository \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/p>\n<pre><code class=\"java\"> List&lt;Client> findByFullNameContaining(String name);<\/code><\/pre>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u0447\u0430\u0441\u0442\u0438 \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClientService \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/p>\n<pre><code class=\"java\"> public List&lt;Client> findByNameContaining(String userName){         return clientRepository.findByFullNameContaining(userName);     }<\/code><\/pre>\n<p>\u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClientController \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/p>\n<pre><code class=\"java\">  @ResponseStatus(HttpStatus.OK)     @GetMapping()     public List&lt;Client> findByNameContaining(@RequestParam String clientName) {         List&lt;Client> clients = clientService.findByNameContaining(clientName);         return clients;     }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1: \u0437\u0430\u0439\u0434\u0435\u043c \u0432 postman \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c get \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>clientName<\/code> \u043d\u0430 <code>http:\/\/localhost:8080\/api\/v1\/client?clientName=Ren<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e36\/db4\/954\/e36db4954a714fea41f2035ae003e730.png\" width=\"962\" height=\"624\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e36\/db4\/954\/e36db4954a714fea41f2035ae003e730.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e hibernate \u0441\u0434\u0435\u043b\u0430\u043b \u0432\u043d\u0430\u0447\u0430\u043b\u0435 <strong>\u043e\u0434\u0438\u043d<\/strong> \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 client \u0438 \u043d\u0430\u0448\u0435\u043b \u0432\u0441\u0435\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0430 \u043f\u043e\u0442\u043e\u043c \u0435\u0449\u0435 <strong>N-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 <\/strong>\u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435 email_address, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 email \u0430\u0434\u0440\u0435\u0441\u0430.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/95c\/728\/fb3\/95c728fb3e6d306c0c253c78509877b2.png\" width=\"477\" height=\"588\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/95c\/728\/fb3\/95c728fb3e6d306c0c253c78509877b2.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1? \u0421\u0443\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u0442\u043e\u043c \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043e <strong>\u043e\u0434\u043d\u043e\u0433\u043e<\/strong>. <\/p>\n<p>\u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>JPA Entity Graph<\/strong>.<\/p>\n<p><strong>Entity Graph &#8212; <\/strong>\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. JPA Entity Graph \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0431\u043e\u0440\u0430, \u0438\u0437\u0431\u0435\u0433\u0430\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.  <\/p>\n<p><strong>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 1<\/strong>. \u041f\u0438\u0448\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e<code><a class=\"mention\" href=\"\/users\/EntityGraph\">@EntityGraph<\/a><\/code> \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u043e\u043c findByFullNameContaining \u0432 ClientRepository.<\/p>\n<pre><code class=\"java\">public interface ClientRepository extends JpaRepository&lt;Client, Long> {     @EntityGraph(type = EntityGraph.EntityGraphType.FETCH, attributePaths = \"emailAddresses\")     List&lt;Client> findByFullNameContaining(String name); }<\/code><\/pre>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044f <code><a class=\"mention\" href=\"\/users\/EntityGraph\">@EntityGraph<\/a><\/code> \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <strong><em>EntityGraphType.FETCH<\/em><\/strong> , \u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u044f \u0435\u0433\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u0438 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e <em>FetchType.EAGER<\/em> \u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u043a emailAddresses.<\/p>\n<p>\u0417\u0430\u0439\u0434\u0435\u043c \u0432 postman \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043d\u043e\u0432\u0430 get \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c clientName \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/client?clientName=Ren<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/84b\/0be\/22e\/84b0be22e7adc77c297484758aa1a318.png\" width=\"429\" height=\"357\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/84b\/0be\/22e\/84b0be22e7adc77c297484758aa1a318.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e <strong>\u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p><strong>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 2. <\/strong> \u041f\u0438\u0448\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code><a class=\"mention\" href=\"\/users\/namedentitygraph\">@NamedEntityGraph<\/a><\/code>\u043d\u0430\u0434 \u043a\u043b\u0430\u0441\u0441\u043e\u043c Client.<\/p>\n<pre><code class=\"java\">@Data @NoArgsConstructor @Entity @Table(name = \"client\") @NamedEntityGraph(name = \"client_entity-graph\", attributeNodes = @NamedAttributeNode(\"emailAddresses\")) public class Client {     @Id     @Column(name = \"id\")     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      @Column(name = \"full_name\")     private String fullName;      @Column(name = \"mobile_number\")     private String mobileNumber;      @OneToMany(cascade = CascadeType.ALL, mappedBy = \"client\")     private List&lt;EmailAddress> emailAddresses;      public Client(String fullName, String mobileNumber, List&lt;EmailAddress> emailAddresses) {         this.fullName = fullName;         this.mobileNumber = mobileNumber;         this.emailAddresses = emailAddresses;     } } <\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f &#171;\u0436\u0430\u0434\u043d\u0430\u044f&#187; \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 emailAddresses.<\/p>\n<p> \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u043d\u0430\u0434 ClientRepository.<\/p>\n<pre><code class=\"java\">public interface ClientRepository extends JpaRepository&lt;Client, Long> {     @EntityGraph(type = EntityGraph.EntityGraphType.FETCH, value = \"client_entity-graph\")     List&lt;Client> findByFullNameContaining(String name); }<\/code><\/pre>\n<p>C\u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043d\u043e\u0432\u0430 get \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>clientName<\/code> \u043d\u0430 <code>http:\/\/localhost:8080\/api\/v1\/client?clientName=Ren<\/code><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/820\/33e\/166\/82033e166164cdb4a779df1d7ee904a9.png\" width=\"372\" height=\"344\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/820\/33e\/166\/82033e166164cdb4a779df1d7ee904a9.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0412\u0441\u0435\u043c \u043a\u0442\u043e \u0434\u043e\u0447\u0438\u0442\u0430\u043b \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u0412\u0441\u0435\u043c \u043f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/714704\/\"> https:\/\/habr.com\/ru\/post\/714704\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0412\u0441\u0435\u043c \u043f\u0440\u0438\u0432\u0435\u0442! \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 N+1 (\u0438\u043b\u0438 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0435\u0435 1+N?) \u0438 \u043a\u0430\u043a \u0435\u0435 \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f EntityGraph.<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u0443 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0442\u043e\u0436\u0435 \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438 hibernate \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u0432\u043d\u0430\u0447\u0430\u043b\u0435 <strong>\u043e\u0434\u0438\u043d (1)<\/strong> <strong>\u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0443\u044e \u043d\u0430\u0441 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c, \u0430 \u043f\u043e\u0442\u043e\u043c <strong>N<\/strong> <strong>\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/strong>, \u0447\u0442\u043e\u0431\u044b \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438. \u0414\u0430\u043d\u043d\u0430\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438\u0437-\u0437\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u0447\u0438\u0441\u043b\u0430 \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u0439 \u043a \u043d\u0435\u0439.<\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code class=\"java\">&lt;dependencies> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-data-jpa&lt;\/artifactId> &lt;\/dependency> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-web&lt;\/artifactId> &lt;\/dependency> &lt;dependency> &lt;groupId>org.postgresql&lt;\/groupId> &lt;artifactId>postgresql&lt;\/artifactId> &lt;scope>runtime&lt;\/scope> &lt;\/dependency> &lt;dependency> &lt;groupId>org.projectlombok&lt;\/groupId> &lt;artifactId>lombok&lt;\/artifactId> &lt;optional>true&lt;\/optional> &lt;\/dependency> &lt;dependency> &lt;groupId>org.springframework.boot&lt;\/groupId> &lt;artifactId>spring-boot-starter-test&lt;\/artifactId> &lt;scope>test&lt;\/scope> &lt;\/dependency> &lt;dependency> &lt;groupId>com.github.javafaker&lt;\/groupId> &lt;artifactId>javafaker&lt;\/artifactId> &lt;version>1.0.2&lt;\/version> &lt;\/dependency> &lt;\/dependencies><\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0434\u0432\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 Client \u0438 EmailAddress.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<pre><code class=\"java\">@Data @NoArgsConstructor @Entity @Table(name = \"client\") public class Client {     @Id     @Column(name = \"id\")     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      @Column(name = \"full_name\")     private String fullName;      @Column(name = \"mobile_number\")     private String mobileNumber;      @OneToMany(cascade = CascadeType.ALL, mappedBy = \"client\")     private List&lt;EmailAddress> emailAddresses;      public Client(String fullName, String mobileNumber, List&lt;EmailAddress> emailAddresses) {         this.fullName = fullName;         this.mobileNumber = mobileNumber;         this.emailAddresses = emailAddresses;     } }<\/code><\/pre>\n<pre><code class=\"java\">@Entity @Table(name = \"email_address\") @Data @NoArgsConstructor public class EmailAddress {     @Id     @Column(name = \"id\")     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      @Column(name = \"email\")     private String email;      @JsonIgnore     @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)     @JoinColumn(name = \"client_id\", referencedColumnName = \"id\")     private Client client;      public EmailAddress(String email) {         this.email = email;     } }<\/code><\/pre>\n<p>\u0421\u0432\u044f\u0437\u044c \u043c\u0435\u0436\u0434\u0443 Client \u0438 EmailAddress <code><a class=\"mention\" href=\"\/users\/OneToMany\">@OneToMany<\/a><\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e email \u0430\u0434\u0440\u0435\u0441\u043e\u0432. <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u043a\u0436\u0435 ClientRepository.<\/p>\n<pre><code class=\"java\">public interface ClientRepository extends JpaRepository&lt;Client, Long> { }<\/code><\/pre>\n<p>\u0412 application.properties \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0447\u0442\u043e\u0431\u044b \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u0432\u044b\u0432\u043e\u0434\u0438\u043b\u0438\u0441\u044c sql \u043a\u043e\u043c\u0430\u043d\u0434\u044b.<\/p>\n<pre><code class=\"java\">spring.datasource.url=jdbc:postgresql:\/\/localhost:5432\/\u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0412\u0430\u0448\u0435\u0439 \u0411\u0414 spring.datasource.username=\u0412\u0430\u0448\u0435 \u0438\u043c\u044f \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a postgres spring.datasource.password=\u0412\u0430\u0448 \u043f\u0430\u0440\u043e\u043b\u044c  spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.database=postgresql  spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.properties.hibernate.format_sql=true<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClientService, \u0433\u0434\u0435 \u0443 \u043d\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u0431\u0438\u0437\u043d\u0435\u0441-\u043b\u043e\u0433\u0438\u043a\u0430. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u043a\u043b\u0430\u0441\u0441\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043d\u0430\u0448\u0443 \u0431\u0430\u0437\u0443. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c 2000 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u0438 \u043f\u0443\u0441\u0442\u044c \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e \u0434\u0432\u0430 email \u0430\u0434\u0440\u0435\u0441\u0430.<\/p>\n<pre><code class=\"java\">@Service public class ClientService {     private final ClientRepository clientRepository;      @Autowired     public ClientService(ClientRepository clientRepository) {         this.clientRepository = clientRepository;     }      public void generateDB(){         List&lt;Client> clients = create2000Clients();         for (int i = 0; i &lt; clients.size(); i++) {             clientRepository.save(clients.get(i));         }     }       public List&lt;Client> create2000Clients() {         List&lt;Client> clients = new ArrayList&lt;>();         Faker faker = new Faker();         for (int i = 0; i &lt; 2_000; i++) {             String firstName = faker.name().firstName();             String lastName = faker.name().lastName();             String sufixTel = String.valueOf(i);             String telephone = \"+375290000000\";              List&lt;EmailAddress>emailAddresses= Arrays.asList(                     new EmailAddress((firstName + lastName).toLowerCase() + \"1\" + i + \"@gmail.com\"),                     new EmailAddress((firstName + lastName).toLowerCase() + \"2\" + i + \"@gmail.com\"));              telephone = telephone.substring(0, telephone.length()-sufixTel.length()) + sufixTel;             Client client = new Client(                     firstName + \" \" + lastName,                     telephone,                     emailAddresses             );              for (EmailAddress emailAddress:emailAddresses) {                 emailAddress.setClient(client);             }              clients.add(client);         }         return clients;     } }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c ClientController, \u0433\u0434\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b.<\/p>\n<pre><code class=\"java\">@RestController @RequestMapping(\"\/api\/v1\/client\") public class ClientController {      private final ClientService clientService;     private final ClientRepository clientRepository;     @Autowired     public ClientController(ClientService clientService, ClientRepository clientRepository) {         this.clientService = clientService;         this.clientRepository = clientRepository;     }      @ResponseStatus(HttpStatus.OK)     @GetMapping(\"\/fillDB\")     public String fillDataBase() {         clientService.generateDB();         return \"Amount clients: \" + clientRepository.count();     }  }<\/code><\/pre>\n<p>\u0427\u0435\u0440\u0435\u0437 postman \u0441\u0434\u0435\u043b\u0430\u0435\u043c get \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/client\/fillDB \u043d\u0430\u0448\u0430 \u0442\u0435\u0441\u0442\u043e\u0432\u0430\u044f \u0431\u0430\u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u043b\u0436\u043d\u0430 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c\u0441\u044f.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c ClientRepository \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/p>\n<pre><code class=\"java\"> List&lt;Client> findByFullNameContaining(String name);<\/code><\/pre>\n<p>\u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u043f\u043e \u0447\u0430\u0441\u0442\u0438 \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClientService \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/p>\n<pre><code class=\"java\"> public List&lt;Client> findByNameContaining(String userName){         return clientRepository.findByFullNameContaining(userName);     }<\/code><\/pre>\n<p>\u0430 \u0442\u0430\u043a\u0436\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043a\u043b\u0430\u0441\u0441 ClientController \u043c\u0435\u0442\u043e\u0434\u043e\u043c<\/p>\n<pre><code class=\"java\">  @ResponseStatus(HttpStatus.OK)     @GetMapping()     public List&lt;Client> findByNameContaining(@RequestParam String clientName) {         List&lt;Client> clients = clientService.findByNameContaining(clientName);         return clients;     }<\/code><\/pre>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1: \u0437\u0430\u0439\u0434\u0435\u043c \u0432 postman \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c get \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>clientName<\/code> \u043d\u0430 <code>http:\/\/localhost:8080\/api\/v1\/client?clientName=Ren<\/code><\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0418 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e hibernate \u0441\u0434\u0435\u043b\u0430\u043b \u0432\u043d\u0430\u0447\u0430\u043b\u0435 <strong>\u043e\u0434\u0438\u043d<\/strong> \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 client \u0438 \u043d\u0430\u0448\u0435\u043b \u0432\u0441\u0435\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0430 \u043f\u043e\u0442\u043e\u043c \u0435\u0449\u0435 <strong>N-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 <\/strong>\u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435 email_address, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043b\u0438\u0435\u043d\u0442\u0430 email \u0430\u0434\u0440\u0435\u0441\u0430.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u0430\u043a \u0440\u0435\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1? \u0421\u0443\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0432 \u0442\u043e\u043c \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u0434\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0433\u043e \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0430, \u0442\u043e \u0435\u0441\u0442\u044c \u0434\u043e <strong>\u043e\u0434\u043d\u043e\u0433\u043e<\/strong>. <\/p>\n<p>\u0415\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u0439, \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <strong>JPA Entity Graph<\/strong>.<\/p>\n<p><strong>Entity Graph &#8212; <\/strong>\u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0445 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u0430. JPA Entity Graph \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u0431\u043e\u0440\u0430, \u0438\u0437\u0431\u0435\u0433\u0430\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u0441\u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0445\u043e\u0440\u043e\u0448\u0438\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u043c \u0434\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439.  <\/p>\n<p><strong>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 1<\/strong>. \u041f\u0438\u0448\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e<code><a class=\"mention\" href=\"\/users\/EntityGraph\">@EntityGraph<\/a><\/code> \u043d\u0430\u0434 \u043c\u0435\u0442\u043e\u0434\u043e\u043c findByFullNameContaining \u0432 ClientRepository.<\/p>\n<pre><code class=\"java\">public interface ClientRepository extends JpaRepository&lt;Client, Long> {     @EntityGraph(type = EntityGraph.EntityGraphType.FETCH, attributePaths = \"emailAddresses\")     List&lt;Client> findByFullNameContaining(String name); }<\/code><\/pre>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044f <code><a class=\"mention\" href=\"\/users\/EntityGraph\">@EntityGraph<\/a><\/code> \u0438\u043c\u0435\u0435\u0442 \u0442\u0438\u043f <strong><em>EntityGraphType.FETCH<\/em><\/strong> , \u043d\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u044f \u0435\u0433\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u0438 \u043e\u043d \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442 \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044e <em>FetchType.EAGER<\/em> \u043a \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u043c, \u0442\u043e \u0435\u0441\u0442\u044c \u043a emailAddresses.<\/p>\n<p>\u0417\u0430\u0439\u0434\u0435\u043c \u0432 postman \u0438 \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043d\u043e\u0432\u0430 get \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c clientName \u043d\u0430 http:\/\/localhost:8080\/api\/v1\/client?clientName=Ren<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e <strong>\u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441<\/strong> \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p><strong>\u0412\u0430\u0440\u0438\u0430\u043d\u0442 2. <\/strong> \u041f\u0438\u0448\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e <code><a class=\"mention\" href=\"\/users\/namedentitygraph\">@NamedEntityGraph<\/a><\/code>\u043d\u0430\u0434 \u043a\u043b\u0430\u0441\u0441\u043e\u043c Client.<\/p>\n<pre><code class=\"java\">@Data @NoArgsConstructor @Entity @Table(name = \"client\") @NamedEntityGraph(name = \"client_entity-graph\", attributeNodes = @NamedAttributeNode(\"emailAddresses\")) public class Client {     @Id     @Column(name = \"id\")     @GeneratedValue(strategy = GenerationType.IDENTITY)     private Long id;      @Column(name = \"full_name\")     private String fullName;      @Column(name = \"mobile_number\")     private String mobileNumber;      @OneToMany(cascade = CascadeType.ALL, mappedBy = \"client\")     private List&lt;EmailAddress> emailAddresses;      public Client(String fullName, String mobileNumber, List&lt;EmailAddress> emailAddresses) {         this.fullName = fullName;         this.mobileNumber = mobileNumber;         this.emailAddresses = emailAddresses;     } } <\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0442\u0430\u043a\u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f &#171;\u0436\u0430\u0434\u043d\u0430\u044f&#187; \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0443\u043a\u0430\u0437\u0430\u043d\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0439 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 emailAddresses.<\/p>\n<p> \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u043d\u0430\u0434 ClientRepository.<\/p>\n<pre><code class=\"java\">public interface ClientRepository extends JpaRepository&lt;Client, Long> {     @EntityGraph(type = EntityGraph.EntityGraphType.FETCH, value = \"client_entity-graph\")     List&lt;Client> findByFullNameContaining(String name); }<\/code><\/pre>\n<p>C\u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043d\u043e\u0432\u0430 get \u0437\u0430\u043f\u0440\u043e\u0441 \u0441 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u043c <code>clientName<\/code> \u043d\u0430 <code>http:\/\/localhost:8080\/api\/v1\/client?clientName=Ren<\/code><\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u041f\u043e\u043b\u0443\u0447\u0438\u043c \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0412\u0441\u0435\u043c \u043a\u0442\u043e \u0434\u043e\u0447\u0438\u0442\u0430\u043b \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0430\u0442\u044c\u044e. \u0412\u0441\u0435\u043c \u043f\u043e\u043a\u0430.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/714704\/\"> https:\/\/habr.com\/ru\/post\/714704\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-344924","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/344924","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344924"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/344924\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344924"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344924"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344924"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}