{"id":313719,"date":"2020-11-24T21:00:48","date_gmt":"2020-11-24T21:00:48","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=313719"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=313719","title":{"rendered":"\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0441 N+1 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u0432 JPA \u0438 Hibernate"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<blockquote>\n<p>\u0412 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u043a\u0443\u0440\u0441\u0430<a href=\"https:\/\/otus.pw\/dEt2\/\"> <\/a><a href=\"https:\/\/otus.pw\/RNSN\/\">&#171;Highload Architect&#187;<\/a><a href=\"https:\/\/otus.pw\/dEt2\/\"> <\/a>\u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a \u043f\u043e \u0442\u0435\u043c\u0435 <a href=\"https:\/\/otus.pw\/dEt2\/\">&#171;\u041f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449&#187;<strong>.<\/strong><\/a><\/p>\n<p>\u0410 \u043f\u043e\u043a\u0430 \u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<\/blockquote>\n<hr>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u0432 \u0447\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 JPA \u0438 Hibernate, \u0438 \u043a\u0430\u043a \u0435\u0435 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c.&nbsp;<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u043d\u0435 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u0430 \u0434\u043b\u044f JPA \u0438 Hibernate, \u0441 \u043d\u0435\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1<\/h3>\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 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 N \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u0445 \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>\u0427\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 N, \u0442\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e \u0438 \u0442\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0418 \u0445\u043e\u0442\u044f <a href=\"https:\/\/vladmihalcea.com\/hibernate-slow-query-log\/\"><u>\u043b\u043e\u0433 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/u><\/a> \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N + 1 \u043e\u043d \u043d\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e.&nbsp;<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u0441\u0443\u043c\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u0432\u043b\u0438\u044f\u044e\u0449\u0435\u0435 \u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0411\u0414: post (\u043f\u043e\u0441\u0442\u044b) \u0438 post_comments (\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043a \u043f\u043e\u0441\u0442\u0430\u043c), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435\u043c <a href=\"https:\/\/vladmihalcea.com\/database-table-relationships\/\"><u>&#171;\u043e\u0434\u0438\u043d-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c&#187;<\/u><\/a>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4ff\/58c\/16a\/4ff58c16a7b8e7cf08d6ee64c17e5bb0\" width=\"812\" height=\"216\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0442\u0430\u0432\u0438\u043c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 post \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code class=\"sql\">INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 1', 1)    INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 2', 2)    INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 3', 3)    INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 4', 4)<\/code><\/pre>\n<p>\u0410 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 <code>post_comment<\/code> \u0447\u0435\u0442\u044b\u0440\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438:<\/p>\n<pre><code class=\"sql\">INSERT INTO post_comment (post_id, review, id) VALUES (1, 'Excellent book to understand Java Persistence', 1)    INSERT INTO post_comment (post_id, review, id) VALUES (2, 'Must-read for Java developers', 2)    INSERT INTO post_comment (post_id, review, id) VALUES (3, 'Five Stars', 3)    INSERT INTO post_comment (post_id, review, id) VALUES (4, 'A great reference book', 4)<\/code><\/pre>\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1 \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c SQL<\/h3>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043b\u044e\u0431\u043e\u0439 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u043f\u0440\u044f\u043c\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 SQL.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0431\u0435\u0440\u0435\u0442\u0435 <code>post_comments<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"sql\">List&lt;Tuple&gt; comments = entityManager.createNativeQuery(\"\"\"     SELECT         pc.id AS id,         pc.review AS review,         pc.post_id AS postId     FROM post_comment pc     \"\"\", Tuple.class) .getResultList();<\/code><\/pre>\n<p>\u0410 \u043f\u043e\u0437\u0436\u0435 \u0440\u0435\u0448\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a (title) \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 (post) \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f (post_comment):<\/p>\n<pre><code class=\"sql\">for (Tuple comment : comments) {     String review = (String) comment.get(\"review\");     Long postId = ((Number) comment.get(\"postId\")).longValue();       String postTitle = (String) entityManager.createNativeQuery(\"\"\"         SELECT             p.title         FROM post p         WHERE p.id = :postId         \"\"\")     .setParameter(\"postId\", postId)     .getSingleResult();       LOGGER.info(         \"The Post '{}' got this review '{}'\",         postTitle,         review     ); }<\/code><\/pre>\n<p>\u0412\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N + 1, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u043f\u044f\u0442\u044c (1 + 4):<\/p>\n<pre><code class=\"sql\">SELECT     pc.id AS id,     pc.review AS review,     pc.post_id AS postId FROM post_comment pc   SELECT p.title FROM post p WHERE p.id = 1 -- The Post 'High-Performance Java Persistence - Part 1' got this review -- 'Excellent book to understand Java Persistence'      SELECT p.title FROM post p WHERE p.id = 2 -- The Post 'High-Performance Java Persistence - Part 2' got this review -- 'Must-read for Java developers'       SELECT p.title FROM post p WHERE p.id = 3 -- The Post 'High-Performance Java Persistence - Part 3' got this review -- 'Five Stars'       SELECT p.title FROM post p WHERE p.id = 4 -- The Post 'High-Performance Java Persistence - Part 4' got this review -- 'A great reference book'<\/code><\/pre>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 N + 1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0434\u043d\u0438\u043c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">List&lt;Tuple&gt; comments = entityManager.createNativeQuery(\"\"\"     SELECT         pc.id AS id,         pc.review AS review,         p.title AS postTitle     FROM post_comment pc     JOIN post p ON pc.post_id = p.id     \"\"\", Tuple.class) .getResultList();   for (Tuple comment : comments) {     String review = (String) comment.get(\"review\");     String postTitle = (String) comment.get(\"postTitle\");       LOGGER.info(         \"The Post '{}' got this review '{}'\",         postTitle,         review     ); }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c.<\/p>\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u0441 JPA \u0438 Hibernate<\/h3>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 JPA \u0438 Hibernate \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N + 1, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u0430\u043f\u044f\u0442\u0441\u044f \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b post \u0438 post_comments:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e55\/a23\/fe3\/e55a23fe3a8942c88c987603fbcef228\" width=\"1519\" height=\"628\"><figcaption><\/figcaption><\/figure>\n<p>JPA-\u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">@Entity(name = \"Post\") @Table(name = \"post\") public class Post {       @Id     private Long id;       private String title;       \/\/Getters and setters omitted for brevity }   @Entity(name = \"PostComment\") @Table(name = \"post_comment\") public class PostComment {       @Id     private Long id;       @ManyToOne     private Post post;       private String review;       \/\/Getters and setters omitted for brevity }<\/code><\/pre>\n<h4>FetchType.EAGER<\/h4>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044f\u0432\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043d\u0435\u044f\u0432\u043d\u043e\u0433\u043e <code>FetchType.EAGER<\/code> \u0434\u043b\u044f JPA-\u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0435\u043c \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f <code>FetchType.EAGER<\/code> \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0432\u0435\u0440\u0436\u0435\u043d\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c N + 1.&nbsp;<\/p>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 <code>@ManyToOne<\/code> \u0438 <code>@OneToOne<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <code>FetchType.EAGER<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u0435\u0441\u043b\u0438 \u0432\u0430\u0448\u0438 \u043c\u0430\u043f\u043f\u0438\u043d\u0433\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">@ManyToOne private Post post;<\/code><\/pre>\n<p>\u0423 \u0432\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>FetchType.EAGER<\/code> \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u0442\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u044c&nbsp;<code>JOIN FETCH<\/code> \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 <code>PostComment<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e JPQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u043b\u0438 Criteria API:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager .createQuery(\"\"\"     select pc     from PostComment pc     \"\"\", PostComment.class) .getResultList();<\/code><\/pre>\n<p>\u0412\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 N + 1:<\/p>\n<pre><code class=\"sql\">SELECT     pc.id AS id1_1_,     pc.post_id AS post_id3_1_,     pc.review AS review2_1_ FROM     post_comment pc   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 1 SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 2 SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 3 SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 4<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b SELECT, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\u043c \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 <code>PostComment<\/code> \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044e \u0441 <code>post<\/code><em>.<\/em><\/p>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>find<\/code> \u0438\u0437 <code>EntityManager<\/code>, \u0432 JPQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445 \u0438 Criteria API \u044f\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043b\u0430\u043d \u0432\u044b\u0431\u043e\u0440\u043a\u0438 (fetch plan), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 Hibernate \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 <code>JOIN FETCH<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u0430\u043c \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f \u0441 <code>post<\/code>, \u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u0435\u0437\u043b\u043e: \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>FetchType.EAGER<\/code> \u043d\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>FetchType.LAZY<\/code>.<\/p>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044e \u0441 <code>post<\/code>, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>JOIN FETCH<\/code>, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 N + 1:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager.createQuery(\"\"\"     select pc     from PostComment pc     join fetch pc.post p     \"\"\", PostComment.class) .getResultList();   for(PostComment comment : comments) {     LOGGER.info(         \"The Post '{}' got this review '{}'\",         comment.getPost().getTitle(),         comment.getReview()     ); }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 Hibernate \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">SELECT     pc.id as id1_1_0_,     pc.post_id as post_id3_1_0_,     pc.review as review2_1_0_,     p.id as id1_0_1_,     p.title as title2_0_1_ FROM     post_comment pc INNER JOIN     post p ON pc.post_id = p.id       -- The Post 'High-Performance Java Persistence - Part 1' got this review -- 'Excellent book to understand Java Persistence'   -- The Post 'High-Performance Java Persistence - Part 2' got this review -- 'Must-read for Java developers'   -- The Post 'High-Performance Java Persistence - Part 3' got this review -- 'Five Stars'   -- The Post 'High-Performance Java Persistence - Part 4' got this review -- 'A great reference book'<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0442\u043e\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 <code>FetchType.EAGER<\/code>, \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 <a href=\"https:\/\/vladmihalcea.com\/eager-fetching-is-a-code-smell\/\"><u>\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/u><\/a>.<\/p>\n<h4>FetchType.LAZY<\/h4>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u044f\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u0442\u0435 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>FetchType.LAZY<\/code> \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439, \u0442\u043e \u0432\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 N + 1.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f \u0441 post \u043c\u0430\u043f\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">@ManyToOne(fetch = FetchType.LAZY) private Post post;<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u0435 <code>PostComment<\/code>:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager .createQuery(\"\"\"     select pc     from PostComment pc     \"\"\", PostComment.class) .getResultList();<\/code><\/pre>\n<p>Hibernate \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">SELECT     pc.id AS id1_1_,     pc.post_id AS post_id3_1_,     pc.review AS review2_1_ FROM     post_comment pc<\/code><\/pre>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u0437\u0436\u0435 \u0432\u044b \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435\u0441\u044c \u043a \u044d\u0442\u043e\u0439 lazy-load \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 \u0441 <code>post<\/code>:<\/p>\n<pre><code class=\"sql\">for(PostComment comment : comments) {     LOGGER.info(         \"The Post '{}' got this review '{}'\",         comment.getPost().getTitle(),         comment.getReview()     ); }<\/code><\/pre>\n<p>\u0412\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 N + 1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c:<\/p>\n<pre><code class=\"sql\">SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 1 -- The Post 'High-Performance Java Persistence - Part 1' got this review -- 'Excellent book to understand Java Persistence'   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 2 -- The Post 'High-Performance Java Persistence - Part 2' got this review -- 'Must-read for Java developers'   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 3 -- The Post 'High-Performance Java Persistence - Part 3' got this review -- 'Five Stars'   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 4 -- The Post 'High-Performance Java Persistence - Part 4' got this review -- 'A great reference book'<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f \u0441 <code>post<\/code> \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f \u043b\u0435\u043d\u0438\u0432\u043e, \u043f\u0440\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0435 \u043a \u044d\u0442\u043e\u0439 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u0443\u0436\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>\u041e\u043f\u044f\u0442\u044c \u0436\u0435, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 <code>JOIN FETCH<\/code> \u043a \u0437\u0430\u043f\u0440\u043e\u0441\u0443 <code>JPQL<\/code>:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager.createQuery(\"\"\"     select pc     from PostComment pc     join fetch pc.post p     \"\"\", PostComment.class) .getResultList();   for(PostComment comment : comments) {     LOGGER.info(         \"The Post '{}' got this review '{}'\",         comment.getPost().getTitle(),         comment.getReview()     ); }<\/code><\/pre>\n<p>\u0418 \u043a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0441 <code>FetchType.EAGER<\/code>, \u044d\u0442\u043e\u0442 JPQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0431\u0443\u0434\u0435\u0442 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441.<\/p>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 <code>FetchType.LAZY<\/code> \u0438 \u043d\u0435 \u0441\u0441\u044b\u043b\u0430\u0435\u0442\u0435\u0441\u044c \u043d\u0430 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 \u0434\u0432\u0443\u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f <code>@OneToOne<\/code>, \u0432\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c N + 1.<\/p>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u043e\u0434\u043e\u043b\u0435\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N+1 c <code>@OneToOne<\/code>-\u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f\u043c\u0438, \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 <a href=\"https:\/\/vladmihalcea.com\/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate\/\"><u>\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/u><\/a>.<\/p>\n<h4>\u041a\u044d\u0448 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f<\/h4>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043a\u044d\u0448\u0430 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0439 \u0438\u043b\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u0435 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 JPQL-\u0437\u0430\u043f\u0440\u043e\u0441, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0439 \u043a\u044d\u0448 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager.createQuery(\"\"\"     select pc     from PostComment pc     order by pc.post.id desc     \"\"\", PostComment.class) .setMaxResults(10) .setHint(QueryHints.HINT_CACHEABLE, true) .getResultList();<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 <code>PostComment<\/code> \u043d\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043a\u044d\u0448\u0435 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0443\u0440\u043e\u0432\u043d\u044f, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e N \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e <code>PostComment<\/code>:<\/p>\n<pre><code class=\"sql\">-- Checking cached query results in region: org.hibernate.cache.internal.StandardQueryCache -- Checking query spaces are up-to-date: [post_comment] -- [post_comment] last update timestamp: 6244574473195524, result set timestamp: 6244574473207808 -- Returning cached query results    SELECT pc.id AS id1_1_0_,        pc.post_id AS post_id3_1_0_,        pc.review AS review2_1_0_ FROM post_comment pc WHERE pc.id = 3    SELECT pc.id AS id1_1_0_,        pc.post_id AS post_id3_1_0_,        pc.review AS review2_1_0_ FROM post_comment pc WHERE pc.id = 2    SELECT pc.id AS id1_1_0_,        pc.post_id AS post_id3_1_0_,        pc.review AS review2_1_0_ FROM post_comment pc WHERE pc.id = 1<\/code><\/pre>\n<p>\u0412 \u043a\u044d\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u044b \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 <code>PostComment<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0435\u0441\u043b\u0438 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0438 <code>PostComment<\/code> \u043d\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u043a\u044d\u0448\u0435, \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u0435\u043d\u044b \u0438\u0437 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0438 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 N \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432.<\/p>\n<hr>\n<blockquote>\n<p><a href=\"https:\/\/otus.pw\/RNSN\/\">\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u043a\u0443\u0440\u0441\u0435 &#171;Highload Architect&#187;<\/a>.<\/p>\n<\/blockquote>\n<\/div>\n<p> \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\/company\/otus\/blog\/529692\/\"> https:\/\/habr.com\/ru\/company\/otus\/blog\/529692\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<blockquote>\n<p>\u0412 \u043f\u0440\u0435\u0434\u0434\u0432\u0435\u0440\u0438\u0438 \u043a\u0443\u0440\u0441\u0430<a href=\"https:\/\/otus.pw\/dEt2\/\"> <\/a><a href=\"https:\/\/otus.pw\/RNSN\/\">&#171;Highload Architect&#187;<\/a><a href=\"https:\/\/otus.pw\/dEt2\/\"> <\/a>\u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0439 \u0443\u0440\u043e\u043a \u043f\u043e \u0442\u0435\u043c\u0435 <a href=\"https:\/\/otus.pw\/dEt2\/\">&#171;\u041f\u0430\u0442\u0442\u0435\u0440\u043d\u044b \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449&#187;<strong>.<\/strong><\/a><\/p>\n<p>\u0410 \u043f\u043e\u043a\u0430 \u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0442\u0440\u0430\u0434\u0438\u0446\u0438\u043e\u043d\u043d\u044b\u043c \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u043e\u043c \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438.<\/p>\n<\/blockquote>\n<hr>\n<h3>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443, \u0432 \u0447\u0435\u043c \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 JPA \u0438 Hibernate, \u0438 \u043a\u0430\u043a \u0435\u0435 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c.&nbsp;<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u043d\u0435 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u0447\u043d\u0430 \u0434\u043b\u044f JPA \u0438 Hibernate, \u0441 \u043d\u0435\u0439 \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0434\u0440\u0443\u0433\u0438\u0445 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0439 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c.<\/p>\n<h3>\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1<\/h3>\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 \u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 N \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0442\u0435\u0445 \u0436\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043e\u0434\u043d\u043e\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430.<\/p>\n<p>\u0427\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 N, \u0442\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u043e \u0438 \u0442\u0435\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u043b\u0438\u044f\u043d\u0438\u0435 \u043d\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u0418 \u0445\u043e\u0442\u044f <a href=\"https:\/\/vladmihalcea.com\/hibernate-slow-query-log\/\"><u>\u043b\u043e\u0433 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432<\/u><\/a> \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043f\u043e\u043c\u043e\u0447\u044c \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b, \u043d\u043e \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N + 1 \u043e\u043d \u043d\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e.&nbsp;<\/p>\n<p>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 \u0437\u0430\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432 \u0441\u0443\u043c\u043c\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u0432\u043b\u0438\u044f\u044e\u0449\u0435\u0435 \u043d\u0430 \u0431\u044b\u0441\u0442\u0440\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0411\u0414: post (\u043f\u043e\u0441\u0442\u044b) \u0438 post_comments (\u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u043a \u043f\u043e\u0441\u0442\u0430\u043c), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u044b \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435\u043c <a href=\"https:\/\/vladmihalcea.com\/database-table-relationships\/\"><u>&#171;\u043e\u0434\u0438\u043d-\u043a\u043e-\u043c\u043d\u043e\u0433\u0438\u043c&#187;<\/u><\/a>:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u0441\u0442\u0430\u0432\u0438\u043c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 post \u0447\u0435\u0442\u044b\u0440\u0435 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<pre><code class=\"sql\">INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 1', 1)    INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 2', 2)    INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 3', 3)    INSERT INTO post (title, id) VALUES ('High-Performance Java Persistence - Part 4', 4)<\/code><\/pre>\n<p>\u0410 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 <code>post_comment<\/code> \u0447\u0435\u0442\u044b\u0440\u0435 \u0434\u043e\u0447\u0435\u0440\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438:<\/p>\n<pre><code class=\"sql\">INSERT INTO post_comment (post_id, review, id) VALUES (1, 'Excellent book to understand Java Persistence', 1)    INSERT INTO post_comment (post_id, review, id) VALUES (2, 'Must-read for Java developers', 2)    INSERT INTO post_comment (post_id, review, id) VALUES (3, 'Five Stars', 3)    INSERT INTO post_comment (post_id, review, id) VALUES (4, 'A great reference book', 4)<\/code><\/pre>\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N+1 \u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u043c SQL<\/h3>\n<p>\u041a\u0430\u043a \u0443\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043b\u043e\u0441\u044c, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043b\u044e\u0431\u043e\u0439 \u0442\u0435\u0445\u043d\u043e\u043b\u043e\u0433\u0438\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0434\u0430\u0436\u0435 \u043f\u0440\u0438 \u043f\u0440\u044f\u043c\u043e\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 SQL.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u044b\u0431\u0435\u0440\u0435\u0442\u0435 <code>post_comments<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430:<\/p>\n<pre><code class=\"sql\">List&lt;Tuple&gt; comments = entityManager.createNativeQuery(\"\"\"     SELECT         pc.id AS id,         pc.review AS review,         pc.post_id AS postId     FROM post_comment pc     \"\"\", Tuple.class) .getResultList();<\/code><\/pre>\n<p>\u0410 \u043f\u043e\u0437\u0436\u0435 \u0440\u0435\u0448\u0438\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a (title) \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0441\u0442\u0430 (post) \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f (post_comment):<\/p>\n<pre><code class=\"sql\">for (Tuple comment : comments) {     String review = (String) comment.get(\"review\");     Long postId = ((Number) comment.get(\"postId\")).longValue();       String postTitle = (String) entityManager.createNativeQuery(\"\"\"         SELECT             p.title         FROM post p         WHERE p.id = :postId         \"\"\")     .setParameter(\"postId\", postId)     .getSingleResult();       LOGGER.info(         \"The Post '{}' got this review '{}'\",         postTitle,         review     ); }<\/code><\/pre>\n<p>\u0412\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N + 1, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0432\u043c\u0435\u0441\u0442\u043e \u043e\u0434\u043d\u043e\u0433\u043e SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u043b\u0438 \u043f\u044f\u0442\u044c (1 + 4):<\/p>\n<pre><code class=\"sql\">SELECT     pc.id AS id,     pc.review AS review,     pc.post_id AS postId FROM post_comment pc   SELECT p.title FROM post p WHERE p.id = 1 -- The Post 'High-Performance Java Persistence - Part 1' got this review -- 'Excellent book to understand Java Persistence'      SELECT p.title FROM post p WHERE p.id = 2 -- The Post 'High-Performance Java Persistence - Part 2' got this review -- 'Must-read for Java developers'       SELECT p.title FROM post p WHERE p.id = 3 -- The Post 'High-Performance Java Persistence - Part 3' got this review -- 'Five Stars'       SELECT p.title FROM post p WHERE p.id = 4 -- The Post 'High-Performance Java Persistence - Part 4' got this review -- 'A great reference book'<\/code><\/pre>\n<p>\u0418\u0441\u043f\u0440\u0430\u0432\u0438\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 N + 1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u043e. \u0412\u0441\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u044d\u0442\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0432\u0441\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0434\u043d\u0438\u043c SQL-\u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">List&lt;Tuple&gt; comments = entityManager.createNativeQuery(\"\"\"     SELECT         pc.id AS id,         pc.review AS review,         p.title AS postTitle     FROM post_comment pc     JOIN post p ON pc.post_id = p.id     \"\"\", Tuple.class) .getResultList();   for (Tuple comment : comments) {     String review = (String) comment.get(\"review\");     String postTitle = (String) comment.get(\"postTitle\");       LOGGER.info(         \"The Post '{}' got this review '{}'\",         postTitle,         review     ); }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0442\u0441\u044f \u0432\u0441\u0435 \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c.<\/p>\n<h3>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430 N + 1 \u0441 JPA \u0438 Hibernate<\/h3>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 JPA \u0438 Hibernate \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 N + 1, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0447\u0435\u043d\u044c \u0432\u0430\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439.<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u043b\u0430\u0441\u0441\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u0430\u043f\u044f\u0442\u0441\u044f \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u044b post \u0438 post_comments:<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>JPA-\u043c\u0430\u043f\u043f\u0438\u043d\u0433 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">@Entity(name = \"Post\") @Table(name = \"post\") public class Post {       @Id     private Long id;       private String title;       \/\/Getters and setters omitted for brevity }   @Entity(name = \"PostComment\") @Table(name = \"post_comment\") public class PostComment {       @Id     private Long id;       @ManyToOne     private Post post;       private String review;       \/\/Getters and setters omitted for brevity }<\/code><\/pre>\n<h4>FetchType.EAGER<\/h4>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u044f\u0432\u043d\u043e\u0433\u043e \u0438\u043b\u0438 \u043d\u0435\u044f\u0432\u043d\u043e\u0433\u043e <code>FetchType.EAGER<\/code> \u0434\u043b\u044f JPA-\u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439 \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0435\u043c \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e. \u0411\u043e\u043b\u0435\u0435 \u0442\u043e\u0433\u043e, \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u044f <code>FetchType.EAGER<\/code> \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0434\u0432\u0435\u0440\u0436\u0435\u043d\u0430 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0430\u043c N + 1.&nbsp;<\/p>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 <code>@ManyToOne<\/code> \u0438 <code>@OneToOne<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442 <code>FetchType.EAGER<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443, \u0435\u0441\u043b\u0438 \u0432\u0430\u0448\u0438 \u043c\u0430\u043f\u043f\u0438\u043d\u0433\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">@ManyToOne private Post post;<\/code><\/pre>\n<p>\u0423 \u0432\u0430\u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <code>FetchType.EAGER<\/code> \u0438 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u0442\u0435 \u0443\u043a\u0430\u0437\u0430\u0442\u044c&nbsp;<code>JOIN FETCH<\/code> \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 <code>PostComment<\/code> \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e JPQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438\u043b\u0438 Criteria API:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager .createQuery(\"\"\"     select pc     from PostComment pc     \"\"\", PostComment.class) .getResultList();<\/code><\/pre>\n<p>\u0412\u044b \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0435\u0442\u0435\u0441\u044c \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 N + 1:<\/p>\n<pre><code class=\"sql\">SELECT     pc.id AS id1_1_,     pc.post_id AS post_id3_1_,     pc.review AS review2_1_ FROM     post_comment pc   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 1 SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 2 SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 3 SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 4<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b SELECT, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u0434 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435\u043c \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 <code>PostComment<\/code> \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044e \u0441 <code>post<\/code><em>.<\/em><\/p>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0445 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>find<\/code> \u0438\u0437 <code>EntityManager<\/code>, \u0432 JPQL-\u0437\u0430\u043f\u0440\u043e\u0441\u0430\u0445 \u0438 Criteria API \u044f\u0432\u043d\u043e \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043b\u0430\u043d \u0432\u044b\u0431\u043e\u0440\u043a\u0438 (fetch plan), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 Hibernate \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c, \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0432 <code>JOIN FETCH<\/code>. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u0430\u043c \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0440\u0443\u0447\u043d\u0443\u044e.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u043d\u0443\u0436\u043d\u0430 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f \u0441 <code>post<\/code>, \u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u0435\u0437\u043b\u043e: \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c <code>FetchType.EAGER<\/code> \u043d\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043b\u0443\u0447\u0448\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>FetchType.LAZY<\/code>.<\/p>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044e \u0441 <code>post<\/code>, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>JOIN FETCH<\/code>, \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u0441 N + 1:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager.createQuery(\"\"\"     select pc     from PostComment pc     join fetch pc.post p     \"\"\", PostComment.class) .getResultList();   for(PostComment comment : comments) {     LOGGER.info(         \"The Post '{}' got this review '{}'\",         comment.getPost().getTitle(),         comment.getReview()     ); }<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 Hibernate \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">SELECT     pc.id as id1_1_0_,     pc.post_id as post_id3_1_0_,     pc.review as review2_1_0_,     p.id as id1_0_1_,     p.title as title2_0_1_ FROM     post_comment pc INNER JOIN     post p ON pc.post_id = p.id       -- The Post 'High-Performance Java Persistence - Part 1' got this review -- 'Excellent book to understand Java Persistence'   -- The Post 'High-Performance Java Persistence - Part 2' got this review -- 'Must-read for Java developers'   -- The Post 'High-Performance Java Persistence - Part 3' got this review -- 'Five Stars'   -- The Post 'High-Performance Java Persistence - Part 4' got this review -- 'A great reference book'<\/code><\/pre>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e \u0442\u043e\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0437\u0431\u0435\u0433\u0430\u0442\u044c \u0441\u0442\u0440\u0430\u0442\u0435\u0433\u0438\u0438 <code>FetchType.EAGER<\/code>, \u0447\u0438\u0442\u0430\u0439\u0442\u0435 \u0432 <a href=\"https:\/\/vladmihalcea.com\/eager-fetching-is-a-code-smell\/\"><u>\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/u><\/a>.<\/p>\n<h4>FetchType.LAZY<\/h4>\n<p>\u0414\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u044f\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u0442\u0435 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 <code>FetchType.LAZY<\/code> \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0439, \u0442\u043e \u0432\u044b \u0432\u0441\u0435 \u0440\u0430\u0432\u043d\u043e \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u0441 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u043e\u0439 N + 1.<\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f \u0441 post \u043c\u0430\u043f\u0438\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code class=\"sql\">@ManyToOne(fetch = FetchType.LAZY) private Post post;<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b \u0437\u0430\u043f\u0440\u043e\u0441\u0438\u0442\u0435 <code>PostComment<\/code>:<\/p>\n<pre><code class=\"sql\">List&lt;PostComment&gt; comments = entityManager .createQuery(\"\"\"     select pc     from PostComment pc     \"\"\", PostComment.class) .getResultList();<\/code><\/pre>\n<p>Hibernate \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u043e\u0434\u0438\u043d SQL-\u0437\u0430\u043f\u0440\u043e\u0441:<\/p>\n<pre><code class=\"sql\">SELECT     pc.id AS id1_1_,     pc.post_id AS post_id3_1_,     pc.review AS review2_1_ FROM     post_comment pc<\/code><\/pre>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u0437\u0436\u0435 \u0432\u044b \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435\u0441\u044c \u043a \u044d\u0442\u043e\u0439 lazy-load \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u0438 \u0441 <code>post<\/code>:<\/p>\n<pre><code class=\"sql\">for(PostComment comment : comments) {     LOGGER.info(         \"The Post '{}' got this review '{}'\",         comment.getPost().getTitle(),         comment.getReview()     ); }<\/code><\/pre>\n<p>\u0412\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441 N + 1 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c:<\/p>\n<pre><code class=\"sql\">SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 1 -- The Post 'High-Performance Java Persistence - Part 1' got this review -- 'Excellent book to understand Java Persistence'   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 2 -- The Post 'High-Performance Java Persistence - Part 2' got this review -- 'Must-read for Java developers'   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 3 -- The Post 'High-Performance Java Persistence - Part 3' got this review -- 'Five Stars'   SELECT p.id AS id1_0_0_, p.title AS title2_0_0_ FROM post p WHERE p.id = 4 -- The Post 'High-Performance Java Persistence - Part 4' got this review -- 'A great reference book'<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0430\u0441\u0441\u043e\u0446\u0438\u0430\u0446\u0438\u044f \u0441 <code>post<\/code><\/p>\n<\/hr>\n<\/blockquote>\n<\/div>\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-313719","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313719","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=313719"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/313719\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=313719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=313719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=313719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}