[{"data":1,"prerenderedAt":2434},["ShallowReactive",2],{"page-en-\u002Fblog\u002Fhibernate-n-plus-one-problem":3,"related-en-\u002Fblog\u002Fhibernate-n-plus-one-problem":2413},{"id":4,"title":5,"body":6,"date":2399,"description":2400,"extension":2401,"meta":2402,"navigation":129,"path":2403,"seo":2404,"stem":2405,"tags":2406,"translation":2411,"__hash__":2412},"pages_en\u002Fen\u002Fblog\u002Fhibernate-n-plus-one-problem.md","Solving the N+1 Query Problem in Hibernate and Spring Data JPA",{"type":7,"value":8,"toc":2383},"minimark",[9,14,23,35,39,42,305,308,424,427,540,562,566,569,607,614,622,625,632,820,823,827,830,906,945,952,956,959,1105,1111,1115,1122,1188,1191,1206,1301,1308,1312,1315,1381,1423,1426,1430,1433,1528,1531,1652,1655,1659,1674,1680,1683,1697,1700,1794,1914,1918,1924,1966,1972,1989,2124,2128,2134,2149,2162,2199,2203,2218,2232,2239,2243,2360,2364,2379],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,17,18,22],"p",{},"Most performance problems in JPA applications come from a single pattern: the ",[19,20,21],"strong",{},"N+1 query problem",". With 10 rows in development nobody notices; in production a list of 1,000 rows sends 1,001 queries to the database and the page takes seconds to load.",[15,24,25,26,30,31,34],{},"This post shows the problem with a ",[27,28,29],"code",{},"Post","\u002F",[27,32,33],{},"Comment"," example, explains how to detect it, and which fix to choose in which situation.",[10,36,38],{"id":37},"_1-what-is-n1","1. What is N+1?",[15,40,41],{},"A simple model:",[43,44,49],"pre",{"className":45,"code":46,"language":47,"meta":48,"style":48},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","@Entity\npublic class Post {\n    @Id @GeneratedValue\n    private Long id;\n    private String title;\n\n    @OneToMany(mappedBy = \"post\")\n    private List\u003CComment> comments = new ArrayList\u003C>();\n}\n\n@Entity\npublic class Comment {\n    @Id @GeneratedValue\n    private Long id;\n    private String text;\n\n    @ManyToOne(fetch = FetchType.LAZY)\n    private Post post;\n}\n","java","",[27,50,51,64,80,95,111,124,131,161,193,199,204,211,223,234,245,257,262,288,300],{"__ignoreMap":48},[52,53,56,60],"span",{"class":54,"line":55},"line",1,[52,57,59],{"class":58},"sMK4o","@",[52,61,63],{"class":62},"spNyl","Entity\n",[52,65,67,70,73,77],{"class":54,"line":66},2,[52,68,69],{"class":62},"public",[52,71,72],{"class":62}," class",[52,74,76],{"class":75},"sBMFI"," Post",[52,78,79],{"class":58}," {\n",[52,81,83,86,89,92],{"class":54,"line":82},3,[52,84,85],{"class":58},"    @",[52,87,88],{"class":62},"Id",[52,90,91],{"class":58}," @",[52,93,94],{"class":62},"GeneratedValue\n",[52,96,98,101,104,108],{"class":54,"line":97},4,[52,99,100],{"class":62},"    private",[52,102,103],{"class":62}," Long",[52,105,107],{"class":106},"sTEyZ"," id",[52,109,110],{"class":58},";\n",[52,112,114,116,119,122],{"class":54,"line":113},5,[52,115,100],{"class":62},[52,117,118],{"class":62}," String",[52,120,121],{"class":106}," title",[52,123,110],{"class":58},[52,125,127],{"class":54,"line":126},6,[52,128,130],{"emptyLinePlaceholder":129},true,"\n",[52,132,134,136,139,142,145,148,151,155,158],{"class":54,"line":133},7,[52,135,85],{"class":58},[52,137,138],{"class":62},"OneToMany",[52,140,141],{"class":58},"(",[52,143,144],{"class":75},"mappedBy",[52,146,147],{"class":58}," =",[52,149,150],{"class":58}," \"",[52,152,154],{"class":153},"sfazB","post",[52,156,157],{"class":58},"\"",[52,159,160],{"class":58},")\n",[52,162,164,166,169,172,174,177,180,183,187,190],{"class":54,"line":163},8,[52,165,100],{"class":62},[52,167,168],{"class":62}," List",[52,170,171],{"class":58},"\u003C",[52,173,33],{"class":62},[52,175,176],{"class":58},">",[52,178,179],{"class":106}," comments ",[52,181,182],{"class":58},"=",[52,184,186],{"class":185},"s7zQu"," new",[52,188,189],{"class":62}," ArrayList",[52,191,192],{"class":58},"\u003C>();\n",[52,194,196],{"class":54,"line":195},9,[52,197,198],{"class":58},"}\n",[52,200,202],{"class":54,"line":201},10,[52,203,130],{"emptyLinePlaceholder":129},[52,205,207,209],{"class":54,"line":206},11,[52,208,59],{"class":58},[52,210,63],{"class":62},[52,212,214,216,218,221],{"class":54,"line":213},12,[52,215,69],{"class":62},[52,217,72],{"class":62},[52,219,220],{"class":75}," Comment",[52,222,79],{"class":58},[52,224,226,228,230,232],{"class":54,"line":225},13,[52,227,85],{"class":58},[52,229,88],{"class":62},[52,231,91],{"class":58},[52,233,94],{"class":62},[52,235,237,239,241,243],{"class":54,"line":236},14,[52,238,100],{"class":62},[52,240,103],{"class":62},[52,242,107],{"class":106},[52,244,110],{"class":58},[52,246,248,250,252,255],{"class":54,"line":247},15,[52,249,100],{"class":62},[52,251,118],{"class":62},[52,253,254],{"class":106}," text",[52,256,110],{"class":58},[52,258,260],{"class":54,"line":259},16,[52,261,130],{"emptyLinePlaceholder":129},[52,263,265,267,270,272,275,277,280,283,286],{"class":54,"line":264},17,[52,266,85],{"class":58},[52,268,269],{"class":62},"ManyToOne",[52,271,141],{"class":58},[52,273,274],{"class":75},"fetch",[52,276,147],{"class":58},[52,278,279],{"class":106}," FetchType",[52,281,282],{"class":58},".",[52,284,285],{"class":106},"LAZY",[52,287,160],{"class":58},[52,289,291,293,295,298],{"class":54,"line":290},18,[52,292,100],{"class":62},[52,294,76],{"class":62},[52,296,297],{"class":106}," post",[52,299,110],{"class":58},[52,301,303],{"class":54,"line":302},19,[52,304,198],{"class":58},[15,306,307],{},"An innocent-looking service method:",[43,309,311],{"className":45,"code":310,"language":47,"meta":48,"style":48},"List\u003CPost> posts = postRepository.findAll();\nfor (Post post : posts) {\n    System.out.println(post.getTitle() + \": \" + post.getComments().size());\n}\n",[27,312,313,341,365,420],{"__ignoreMap":48},[52,314,315,318,320,322,324,327,329,332,334,338],{"class":54,"line":55},[52,316,317],{"class":62},"List",[52,319,171],{"class":58},[52,321,29],{"class":62},[52,323,176],{"class":58},[52,325,326],{"class":106}," posts ",[52,328,182],{"class":58},[52,330,331],{"class":106}," postRepository",[52,333,282],{"class":58},[52,335,337],{"class":336},"s2Zo4","findAll",[52,339,340],{"class":58},"();\n",[52,342,343,346,349,351,354,357,360,363],{"class":54,"line":66},[52,344,345],{"class":185},"for",[52,347,348],{"class":58}," (",[52,350,29],{"class":62},[52,352,353],{"class":106}," post ",[52,355,356],{"class":185},":",[52,358,359],{"class":106}," posts",[52,361,362],{"class":58},")",[52,364,79],{"class":58},[52,366,367,370,372,375,377,380,382,384,386,389,392,395,397,400,402,404,406,408,411,414,417],{"class":54,"line":82},[52,368,369],{"class":106},"    System",[52,371,282],{"class":58},[52,373,374],{"class":106},"out",[52,376,282],{"class":58},[52,378,379],{"class":336},"println",[52,381,141],{"class":58},[52,383,154],{"class":106},[52,385,282],{"class":58},[52,387,388],{"class":336},"getTitle",[52,390,391],{"class":58},"()",[52,393,394],{"class":58}," +",[52,396,150],{"class":58},[52,398,399],{"class":153},": ",[52,401,157],{"class":58},[52,403,394],{"class":58},[52,405,297],{"class":106},[52,407,282],{"class":58},[52,409,410],{"class":336},"getComments",[52,412,413],{"class":58},"().",[52,415,416],{"class":336},"size",[52,418,419],{"class":58},"());\n",[52,421,422],{"class":54,"line":97},[52,423,198],{"class":58},[15,425,426],{},"The SQL log:",[43,428,432],{"className":429,"code":430,"language":431,"meta":48,"style":48},"language-sql shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","select p.id, p.title from post p;                       -- 1 query\nselect c.* from comment c where c.post_id = 1;          -- one per post\nselect c.* from comment c where c.post_id = 2;          -- and another\nselect c.* from comment c where c.post_id = 3;\n-- ... N in total\n","sql",[27,433,434,453,486,512,535],{"__ignoreMap":48},[52,435,436,440,443,446,449],{"class":54,"line":55},[52,437,439],{"class":438},"sbssI","select",[52,441,442],{"class":106}," p.id, p.title ",[52,444,445],{"class":438},"from",[52,447,448],{"class":106}," post p;                       ",[52,450,452],{"class":451},"sHwdD","-- 1 query\n",[52,454,455,457,460,463,466,469,472,475,477,480,483],{"class":54,"line":66},[52,456,439],{"class":438},[52,458,459],{"class":106}," c.",[52,461,462],{"class":58},"*",[52,464,465],{"class":438}," from",[52,467,468],{"class":106}," comment c ",[52,470,471],{"class":438},"where",[52,473,474],{"class":106}," c.post_id ",[52,476,182],{"class":58},[52,478,479],{"class":438}," 1",[52,481,482],{"class":106},";          ",[52,484,485],{"class":451},"-- one per post\n",[52,487,488,490,492,494,496,498,500,502,504,507,509],{"class":54,"line":82},[52,489,439],{"class":438},[52,491,459],{"class":106},[52,493,462],{"class":58},[52,495,465],{"class":438},[52,497,468],{"class":106},[52,499,471],{"class":438},[52,501,474],{"class":106},[52,503,182],{"class":58},[52,505,506],{"class":438}," 2",[52,508,482],{"class":106},[52,510,511],{"class":451},"-- and another\n",[52,513,514,516,518,520,522,524,526,528,530,533],{"class":54,"line":97},[52,515,439],{"class":438},[52,517,459],{"class":106},[52,519,462],{"class":58},[52,521,465],{"class":438},[52,523,468],{"class":106},[52,525,471],{"class":438},[52,527,474],{"class":106},[52,529,182],{"class":58},[52,531,532],{"class":438}," 3",[52,534,110],{"class":106},[52,536,537],{"class":54,"line":113},[52,538,539],{"class":451},"-- ... N in total\n",[15,541,542,543,546,547,550,551,554,555,558,559,561],{},"One query loads the list (",[19,544,545],{},"1","), then accessing each post's lazy collection runs a separate query (",[19,548,549],{},"N","). The same happens on the ",[27,552,553],{},"@ManyToOne"," side: ",[27,556,557],{},"comment.getPost().getTitle()"," issues a separate ",[27,560,439],{}," for every post not already in the persistence context.",[10,563,565],{"id":564},"_2-detecting-the-problem","2. Detecting the problem",[15,567,568],{},"The first step is to see the SQL that actually runs:",[43,570,574],{"className":571,"code":572,"language":573,"meta":48,"style":48},"language-properties shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","logging.level.org.hibernate.SQL=debug\nlogging.level.org.hibernate.orm.jdbc.bind=trace\nspring.jpa.properties.hibernate.generate_statistics=true\n","properties",[27,575,576,587,597],{"__ignoreMap":48},[52,577,578,582,584],{"class":54,"line":55},[52,579,581],{"class":580},"swJcz","logging.level.org.hibernate.SQL",[52,583,182],{"class":58},[52,585,586],{"class":106},"debug\n",[52,588,589,592,594],{"class":54,"line":66},[52,590,591],{"class":580},"logging.level.org.hibernate.orm.jdbc.bind",[52,593,182],{"class":58},[52,595,596],{"class":106},"trace\n",[52,598,599,602,604],{"class":54,"line":82},[52,600,601],{"class":580},"spring.jpa.properties.hibernate.generate_statistics",[52,603,182],{"class":58},[52,605,606],{"class":106},"true\n",[15,608,609,610,613],{},"With ",[27,611,612],{},"generate_statistics"," on, a summary like this is logged at the end of each session:",[43,615,620],{"className":616,"code":618,"language":619,"meta":48},[617],"language-text","StatisticalLoggingSessionEventListener : Session Metrics {\n    1203400 nanoseconds spent preparing 101 JDBC statements;\n    ...\n}\n","text",[27,621,618],{"__ignoreMap":48},[15,623,624],{},"101 statements for a single HTTP request is the clearest sign of N+1.",[15,626,627,628,631],{},"Even better, assert the query count in a test. Hibernate's ",[27,629,630],{},"Statistics"," API is enough for that:",[43,633,635],{"className":45,"code":634,"language":47,"meta":48,"style":48},"@SpringBootTest\n@Transactional\nclass PostServiceTest {\n\n    @Autowired EntityManagerFactory emf;\n    @Autowired PostService postService;\n\n    @Test\n    void listingPostsUsesSingleQuery() {\n        Statistics stats = emf.unwrap(SessionFactory.class).getStatistics();\n        stats.clear();\n\n        postService.findAllWithComments();\n\n        assertThat(stats.getPrepareStatementCount()).isEqualTo(1);\n    }\n}\n",[27,636,637,644,651,661,665,680,694,698,705,717,751,763,767,779,783,811,816],{"__ignoreMap":48},[52,638,639,641],{"class":54,"line":55},[52,640,59],{"class":58},[52,642,643],{"class":62},"SpringBootTest\n",[52,645,646,648],{"class":54,"line":66},[52,647,59],{"class":58},[52,649,650],{"class":62},"Transactional\n",[52,652,653,656,659],{"class":54,"line":82},[52,654,655],{"class":62},"class",[52,657,658],{"class":75}," PostServiceTest",[52,660,79],{"class":58},[52,662,663],{"class":54,"line":97},[52,664,130],{"emptyLinePlaceholder":129},[52,666,667,669,672,675,678],{"class":54,"line":113},[52,668,85],{"class":58},[52,670,671],{"class":62},"Autowired",[52,673,674],{"class":62}," EntityManagerFactory",[52,676,677],{"class":106}," emf",[52,679,110],{"class":58},[52,681,682,684,686,689,692],{"class":54,"line":126},[52,683,85],{"class":58},[52,685,671],{"class":62},[52,687,688],{"class":62}," PostService",[52,690,691],{"class":106}," postService",[52,693,110],{"class":58},[52,695,696],{"class":54,"line":133},[52,697,130],{"emptyLinePlaceholder":129},[52,699,700,702],{"class":54,"line":163},[52,701,85],{"class":58},[52,703,704],{"class":62},"Test\n",[52,706,707,710,713,715],{"class":54,"line":195},[52,708,709],{"class":62},"    void",[52,711,712],{"class":336}," listingPostsUsesSingleQuery",[52,714,391],{"class":58},[52,716,79],{"class":58},[52,718,719,722,725,727,729,731,734,736,739,741,743,746,749],{"class":54,"line":201},[52,720,721],{"class":62},"        Statistics",[52,723,724],{"class":106}," stats ",[52,726,182],{"class":58},[52,728,677],{"class":106},[52,730,282],{"class":58},[52,732,733],{"class":336},"unwrap",[52,735,141],{"class":58},[52,737,738],{"class":106},"SessionFactory",[52,740,282],{"class":58},[52,742,655],{"class":106},[52,744,745],{"class":58},").",[52,747,748],{"class":336},"getStatistics",[52,750,340],{"class":58},[52,752,753,756,758,761],{"class":54,"line":206},[52,754,755],{"class":106},"        stats",[52,757,282],{"class":58},[52,759,760],{"class":336},"clear",[52,762,340],{"class":58},[52,764,765],{"class":54,"line":213},[52,766,130],{"emptyLinePlaceholder":129},[52,768,769,772,774,777],{"class":54,"line":225},[52,770,771],{"class":106},"        postService",[52,773,282],{"class":58},[52,775,776],{"class":336},"findAllWithComments",[52,778,340],{"class":58},[52,780,781],{"class":54,"line":236},[52,782,130],{"emptyLinePlaceholder":129},[52,784,785,788,790,793,795,798,801,804,806,808],{"class":54,"line":247},[52,786,787],{"class":336},"        assertThat",[52,789,141],{"class":58},[52,791,792],{"class":106},"stats",[52,794,282],{"class":58},[52,796,797],{"class":336},"getPrepareStatementCount",[52,799,800],{"class":58},"()).",[52,802,803],{"class":336},"isEqualTo",[52,805,141],{"class":58},[52,807,545],{"class":438},[52,809,810],{"class":58},");\n",[52,812,813],{"class":54,"line":259},[52,814,815],{"class":58},"    }\n",[52,817,818],{"class":54,"line":264},[52,819,198],{"class":58},[15,821,822],{},"This test catches the regression as soon as someone changes the repository method later.",[10,824,826],{"id":825},"_3-fix-join-fetch","3. Fix: JOIN FETCH",[15,828,829],{},"The most direct fix is to load the association in the same query:",[43,831,833],{"className":45,"code":832,"language":47,"meta":48,"style":48},"public interface PostRepository extends JpaRepository\u003CPost, Long> {\n\n    @Query(\"select distinct p from Post p left join fetch p.comments\")\n    List\u003CPost> findAllWithComments();\n}\n",[27,834,835,864,868,886,902],{"__ignoreMap":48},[52,836,837,839,842,845,848,851,853,855,858,860,862],{"class":54,"line":55},[52,838,69],{"class":62},[52,840,841],{"class":62}," interface",[52,843,844],{"class":75}," PostRepository",[52,846,847],{"class":62}," extends",[52,849,850],{"class":75}," JpaRepository",[52,852,171],{"class":58},[52,854,29],{"class":62},[52,856,857],{"class":58},",",[52,859,103],{"class":62},[52,861,176],{"class":58},[52,863,79],{"class":58},[52,865,866],{"class":54,"line":66},[52,867,130],{"emptyLinePlaceholder":129},[52,869,870,872,875,877,879,882,884],{"class":54,"line":82},[52,871,85],{"class":58},[52,873,874],{"class":62},"Query",[52,876,141],{"class":58},[52,878,157],{"class":58},[52,880,881],{"class":153},"select distinct p from Post p left join fetch p.comments",[52,883,157],{"class":58},[52,885,160],{"class":58},[52,887,888,891,893,895,897,900],{"class":54,"line":97},[52,889,890],{"class":62},"    List",[52,892,171],{"class":58},[52,894,29],{"class":62},[52,896,176],{"class":58},[52,898,899],{"class":336}," findAllWithComments",[52,901,340],{"class":58},[52,903,904],{"class":54,"line":113},[52,905,198],{"class":58},[43,907,909],{"className":429,"code":908,"language":431,"meta":48,"style":48},"select p.*, c.* from post p left join comment c on c.post_id = p.id;\n",[27,910,911],{"__ignoreMap":48},[52,912,913,915,918,920,923,925,927,930,933,935,938,940,942],{"class":54,"line":55},[52,914,439],{"class":438},[52,916,917],{"class":106}," p.",[52,919,462],{"class":58},[52,921,922],{"class":106},", c.",[52,924,462],{"class":58},[52,926,465],{"class":438},[52,928,929],{"class":106}," post p ",[52,931,932],{"class":438},"left join",[52,934,468],{"class":106},[52,936,937],{"class":438},"on",[52,939,474],{"class":106},[52,941,182],{"class":58},[52,943,944],{"class":106}," p.id;\n",[15,946,947,948,951],{},"Since Hibernate 6 entity results are de-duplicated automatically, so ",[27,949,950],{},"distinct"," is no longer required, but there's no harm in writing it to state the intent.",[10,953,955],{"id":954},"_4-fix-entitygraph","4. Fix: @EntityGraph",[15,957,958],{},"To add a fetch plan to Spring Data derived methods without writing JPQL:",[43,960,962],{"className":45,"code":961,"language":47,"meta":48,"style":48},"public interface PostRepository extends JpaRepository\u003CPost, Long> {\n\n    @EntityGraph(attributePaths = {\"comments\"})\n    List\u003CPost> findByTitleContaining(String keyword);\n\n    @EntityGraph(attributePaths = {\"comments\", \"author\"})\n    Optional\u003CPost> findWithDetailsById(Long id);\n}\n",[27,963,964,988,992,1019,1043,1047,1078,1101],{"__ignoreMap":48},[52,965,966,968,970,972,974,976,978,980,982,984,986],{"class":54,"line":55},[52,967,69],{"class":62},[52,969,841],{"class":62},[52,971,844],{"class":75},[52,973,847],{"class":62},[52,975,850],{"class":75},[52,977,171],{"class":58},[52,979,29],{"class":62},[52,981,857],{"class":58},[52,983,103],{"class":62},[52,985,176],{"class":58},[52,987,79],{"class":58},[52,989,990],{"class":54,"line":66},[52,991,130],{"emptyLinePlaceholder":129},[52,993,994,996,999,1001,1004,1006,1009,1011,1014,1016],{"class":54,"line":82},[52,995,85],{"class":58},[52,997,998],{"class":62},"EntityGraph",[52,1000,141],{"class":58},[52,1002,1003],{"class":75},"attributePaths",[52,1005,147],{"class":58},[52,1007,1008],{"class":58}," {",[52,1010,157],{"class":58},[52,1012,1013],{"class":153},"comments",[52,1015,157],{"class":58},[52,1017,1018],{"class":58},"})\n",[52,1020,1021,1023,1025,1027,1029,1032,1034,1037,1041],{"class":54,"line":97},[52,1022,890],{"class":62},[52,1024,171],{"class":58},[52,1026,29],{"class":62},[52,1028,176],{"class":58},[52,1030,1031],{"class":336}," findByTitleContaining",[52,1033,141],{"class":58},[52,1035,1036],{"class":62},"String",[52,1038,1040],{"class":1039},"sHdIc"," keyword",[52,1042,810],{"class":58},[52,1044,1045],{"class":54,"line":113},[52,1046,130],{"emptyLinePlaceholder":129},[52,1048,1049,1051,1053,1055,1057,1059,1061,1063,1065,1067,1069,1071,1074,1076],{"class":54,"line":126},[52,1050,85],{"class":58},[52,1052,998],{"class":62},[52,1054,141],{"class":58},[52,1056,1003],{"class":75},[52,1058,147],{"class":58},[52,1060,1008],{"class":58},[52,1062,157],{"class":58},[52,1064,1013],{"class":153},[52,1066,157],{"class":58},[52,1068,857],{"class":58},[52,1070,150],{"class":58},[52,1072,1073],{"class":153},"author",[52,1075,157],{"class":58},[52,1077,1018],{"class":58},[52,1079,1080,1083,1085,1087,1089,1092,1094,1097,1099],{"class":54,"line":133},[52,1081,1082],{"class":62},"    Optional",[52,1084,171],{"class":58},[52,1086,29],{"class":62},[52,1088,176],{"class":58},[52,1090,1091],{"class":336}," findWithDetailsById",[52,1093,141],{"class":58},[52,1095,1096],{"class":62},"Long",[52,1098,107],{"class":1039},[52,1100,810],{"class":58},[52,1102,1103],{"class":54,"line":163},[52,1104,198],{"class":58},[15,1106,1107,1110],{},[27,1108,1109],{},"@EntityGraph"," loads the associations with a join; they stay lazy on the entity and are only eager for this method. That's much safer than making them eager everywhere.",[10,1112,1114],{"id":1113},"_5-fix-batchsize-and-default_batch_fetch_size","5. Fix: @BatchSize and default_batch_fetch_size",[15,1116,1117,1118,1121],{},"Sometimes you don't want a join (large collections, pagination). Batch fetching turns N queries into ",[27,1119,1120],{},"N \u002F batchSize"," queries:",[43,1123,1125],{"className":45,"code":1124,"language":47,"meta":48,"style":48},"@OneToMany(mappedBy = \"post\")\n@BatchSize(size = 50)\nprivate List\u003CComment> comments = new ArrayList\u003C>();\n",[27,1126,1127,1147,1165],{"__ignoreMap":48},[52,1128,1129,1131,1133,1135,1137,1139,1141,1143,1145],{"class":54,"line":55},[52,1130,59],{"class":58},[52,1132,138],{"class":62},[52,1134,141],{"class":58},[52,1136,144],{"class":75},[52,1138,147],{"class":58},[52,1140,150],{"class":58},[52,1142,154],{"class":153},[52,1144,157],{"class":58},[52,1146,160],{"class":58},[52,1148,1149,1151,1154,1156,1158,1160,1163],{"class":54,"line":66},[52,1150,59],{"class":58},[52,1152,1153],{"class":62},"BatchSize",[52,1155,141],{"class":58},[52,1157,416],{"class":75},[52,1159,147],{"class":58},[52,1161,1162],{"class":438}," 50",[52,1164,160],{"class":58},[52,1166,1167,1170,1172,1174,1176,1178,1180,1182,1184,1186],{"class":54,"line":82},[52,1168,1169],{"class":62},"private",[52,1171,168],{"class":62},[52,1173,171],{"class":58},[52,1175,33],{"class":62},[52,1177,176],{"class":58},[52,1179,179],{"class":106},[52,1181,182],{"class":58},[52,1183,186],{"class":185},[52,1185,189],{"class":62},[52,1187,192],{"class":58},[15,1189,1190],{},"Or globally for the whole application:",[43,1192,1194],{"className":571,"code":1193,"language":573,"meta":48,"style":48},"spring.jpa.properties.hibernate.default_batch_fetch_size=50\n",[27,1195,1196],{"__ignoreMap":48},[52,1197,1198,1201,1203],{"class":54,"line":55},[52,1199,1200],{"class":580},"spring.jpa.properties.hibernate.default_batch_fetch_size",[52,1202,182],{"class":58},[52,1204,1205],{"class":106},"50\n",[43,1207,1209],{"className":429,"code":1208,"language":431,"meta":48,"style":48},"select p.* from post p;\nselect c.* from comment c where c.post_id in (1, 2, 3, ..., 50);\nselect c.* from comment c where c.post_id in (51, 52, ..., 100);\n",[27,1210,1211,1224,1266],{"__ignoreMap":48},[52,1212,1213,1215,1217,1219,1221],{"class":54,"line":55},[52,1214,439],{"class":438},[52,1216,917],{"class":106},[52,1218,462],{"class":58},[52,1220,465],{"class":438},[52,1222,1223],{"class":106}," post p;\n",[52,1225,1226,1228,1230,1232,1234,1236,1238,1240,1243,1245,1247,1250,1253,1255,1258,1261,1264],{"class":54,"line":66},[52,1227,439],{"class":438},[52,1229,459],{"class":106},[52,1231,462],{"class":58},[52,1233,465],{"class":438},[52,1235,468],{"class":106},[52,1237,471],{"class":438},[52,1239,474],{"class":106},[52,1241,1242],{"class":438},"in",[52,1244,348],{"class":106},[52,1246,545],{"class":438},[52,1248,1249],{"class":106},", ",[52,1251,1252],{"class":438},"2",[52,1254,1249],{"class":106},[52,1256,1257],{"class":438},"3",[52,1259,1260],{"class":106},", ..., ",[52,1262,1263],{"class":438},"50",[52,1265,810],{"class":106},[52,1267,1268,1270,1272,1274,1276,1278,1280,1282,1284,1286,1289,1291,1294,1296,1299],{"class":54,"line":82},[52,1269,439],{"class":438},[52,1271,459],{"class":106},[52,1273,462],{"class":58},[52,1275,465],{"class":438},[52,1277,468],{"class":106},[52,1279,471],{"class":438},[52,1281,474],{"class":106},[52,1283,1242],{"class":438},[52,1285,348],{"class":106},[52,1287,1288],{"class":438},"51",[52,1290,1249],{"class":106},[52,1292,1293],{"class":438},"52",[52,1295,1260],{"class":106},[52,1297,1298],{"class":438},"100",[52,1300,810],{"class":106},[15,1302,1303,1304,1307],{},"3 queries instead of 101 for 100 posts. ",[27,1305,1306],{},"default_batch_fetch_size"," is a low-risk setting worth enabling in almost every project.",[10,1309,1311],{"id":1310},"_6-fix-fetchfetchmodesubselect","6. Fix: @Fetch(FetchMode.SUBSELECT)",[15,1313,1314],{},"This Hibernate-specific option loads all collections at once, reusing the first query as a subquery:",[43,1316,1318],{"className":45,"code":1317,"language":47,"meta":48,"style":48},"@OneToMany(mappedBy = \"post\")\n@Fetch(FetchMode.SUBSELECT)\nprivate List\u003CComment> comments = new ArrayList\u003C>();\n",[27,1319,1320,1340,1359],{"__ignoreMap":48},[52,1321,1322,1324,1326,1328,1330,1332,1334,1336,1338],{"class":54,"line":55},[52,1323,59],{"class":58},[52,1325,138],{"class":62},[52,1327,141],{"class":58},[52,1329,144],{"class":75},[52,1331,147],{"class":58},[52,1333,150],{"class":58},[52,1335,154],{"class":153},[52,1337,157],{"class":58},[52,1339,160],{"class":58},[52,1341,1342,1344,1347,1349,1352,1354,1357],{"class":54,"line":66},[52,1343,59],{"class":58},[52,1345,1346],{"class":62},"Fetch",[52,1348,141],{"class":58},[52,1350,1351],{"class":106},"FetchMode",[52,1353,282],{"class":58},[52,1355,1356],{"class":106},"SUBSELECT",[52,1358,160],{"class":58},[52,1360,1361,1363,1365,1367,1369,1371,1373,1375,1377,1379],{"class":54,"line":82},[52,1362,1169],{"class":62},[52,1364,168],{"class":62},[52,1366,171],{"class":58},[52,1368,33],{"class":62},[52,1370,176],{"class":58},[52,1372,179],{"class":106},[52,1374,182],{"class":58},[52,1376,186],{"class":185},[52,1378,189],{"class":62},[52,1380,192],{"class":58},[43,1382,1384],{"className":429,"code":1383,"language":431,"meta":48,"style":48},"select c.* from comment c\nwhere c.post_id in (select p.id from post p where ...);\n",[27,1385,1386,1399],{"__ignoreMap":48},[52,1387,1388,1390,1392,1394,1396],{"class":54,"line":55},[52,1389,439],{"class":438},[52,1391,459],{"class":106},[52,1393,462],{"class":58},[52,1395,465],{"class":438},[52,1397,1398],{"class":106}," comment c\n",[52,1400,1401,1403,1405,1407,1409,1411,1414,1416,1418,1420],{"class":54,"line":66},[52,1402,471],{"class":438},[52,1404,474],{"class":106},[52,1406,1242],{"class":438},[52,1408,348],{"class":106},[52,1410,439],{"class":438},[52,1412,1413],{"class":106}," p.id ",[52,1415,445],{"class":438},[52,1417,929],{"class":106},[52,1419,471],{"class":438},[52,1421,1422],{"class":106}," ...);\n",[15,1424,1425],{},"2 queries in total. If the first query is expensive, remember it runs again as the subquery.",[10,1427,1429],{"id":1428},"_7-fix-dto-and-record-projections","7. Fix: DTO and record projections",[15,1431,1432],{},"If a screen only shows a few fields, you don't need to load entities at all. Projections remove both the N+1 and the unnecessary column reads:",[43,1434,1436],{"className":45,"code":1435,"language":47,"meta":48,"style":48},"public record PostSummary(Long id, String title, long commentCount) { }\n\n@Query(\"\"\"\n       select new com.example.blog.PostSummary(p.id, p.title, count(c))\n       from Post p left join p.comments c\n       group by p.id, p.title\n       \"\"\")\nList\u003CPostSummary> findSummaries();\n",[27,1437,1438,1475,1479,1490,1495,1500,1505,1512],{"__ignoreMap":48},[52,1439,1440,1442,1445,1448,1450,1452,1454,1456,1458,1460,1462,1465,1468,1470,1472],{"class":54,"line":55},[52,1441,69],{"class":62},[52,1443,1444],{"class":62}," record",[52,1446,1447],{"class":75}," PostSummary",[52,1449,141],{"class":58},[52,1451,1096],{"class":62},[52,1453,107],{"class":106},[52,1455,857],{"class":58},[52,1457,118],{"class":62},[52,1459,121],{"class":106},[52,1461,857],{"class":58},[52,1463,1464],{"class":62}," long",[52,1466,1467],{"class":106}," commentCount",[52,1469,362],{"class":58},[52,1471,1008],{"class":58},[52,1473,1474],{"class":58}," }\n",[52,1476,1477],{"class":54,"line":66},[52,1478,130],{"emptyLinePlaceholder":129},[52,1480,1481,1483,1485,1487],{"class":54,"line":82},[52,1482,59],{"class":58},[52,1484,874],{"class":62},[52,1486,141],{"class":58},[52,1488,1489],{"class":58},"\"\"\"\n",[52,1491,1492],{"class":54,"line":97},[52,1493,1494],{"class":153},"       select new com.example.blog.PostSummary(p.id, p.title, count(c))\n",[52,1496,1497],{"class":54,"line":113},[52,1498,1499],{"class":153},"       from Post p left join p.comments c\n",[52,1501,1502],{"class":54,"line":126},[52,1503,1504],{"class":153},"       group by p.id, p.title\n",[52,1506,1507,1510],{"class":54,"line":133},[52,1508,1509],{"class":58},"       \"\"\"",[52,1511,160],{"class":58},[52,1513,1514,1516,1518,1521,1523,1526],{"class":54,"line":163},[52,1515,317],{"class":62},[52,1517,171],{"class":58},[52,1519,1520],{"class":106},"PostSummary",[52,1522,176],{"class":58},[52,1524,1525],{"class":336}," findSummaries",[52,1527,340],{"class":58},[15,1529,1530],{},"Spring Data also supports interface and record projections:",[43,1532,1534],{"className":45,"code":1533,"language":47,"meta":48,"style":48},"public interface PostTitle {\n    Long getId();\n    String getTitle();\n}\n\nList\u003CPostTitle> findByTitleStartingWith(String prefix);   \u002F\u002F interface projection\n\nrecord PostView(Long id, String title) { }\nList\u003CPostView> findAllBy();                             \u002F\u002F record (class-based) projection\n",[27,1535,1536,1547,1557,1567,1571,1575,1602,1606,1632],{"__ignoreMap":48},[52,1537,1538,1540,1542,1545],{"class":54,"line":55},[52,1539,69],{"class":62},[52,1541,841],{"class":62},[52,1543,1544],{"class":75}," PostTitle",[52,1546,79],{"class":58},[52,1548,1549,1552,1555],{"class":54,"line":66},[52,1550,1551],{"class":62},"    Long",[52,1553,1554],{"class":336}," getId",[52,1556,340],{"class":58},[52,1558,1559,1562,1565],{"class":54,"line":82},[52,1560,1561],{"class":62},"    String",[52,1563,1564],{"class":336}," getTitle",[52,1566,340],{"class":58},[52,1568,1569],{"class":54,"line":97},[52,1570,198],{"class":58},[52,1572,1573],{"class":54,"line":113},[52,1574,130],{"emptyLinePlaceholder":129},[52,1576,1577,1579,1581,1584,1586,1589,1591,1593,1596,1599],{"class":54,"line":126},[52,1578,317],{"class":62},[52,1580,171],{"class":58},[52,1582,1583],{"class":106},"PostTitle",[52,1585,176],{"class":58},[52,1587,1588],{"class":336}," findByTitleStartingWith",[52,1590,141],{"class":58},[52,1592,1036],{"class":62},[52,1594,1595],{"class":106}," prefix",[52,1597,1598],{"class":58},");",[52,1600,1601],{"class":451},"   \u002F\u002F interface projection\n",[52,1603,1604],{"class":54,"line":133},[52,1605,130],{"emptyLinePlaceholder":129},[52,1607,1608,1611,1614,1616,1618,1620,1622,1624,1626,1628,1630],{"class":54,"line":163},[52,1609,1610],{"class":62},"record",[52,1612,1613],{"class":75}," PostView",[52,1615,141],{"class":58},[52,1617,1096],{"class":62},[52,1619,107],{"class":106},[52,1621,857],{"class":58},[52,1623,118],{"class":62},[52,1625,121],{"class":106},[52,1627,362],{"class":58},[52,1629,1008],{"class":58},[52,1631,1474],{"class":58},[52,1633,1634,1636,1638,1641,1643,1646,1649],{"class":54,"line":195},[52,1635,317],{"class":62},[52,1637,171],{"class":58},[52,1639,1640],{"class":106},"PostView",[52,1642,176],{"class":58},[52,1644,1645],{"class":336}," findAllBy",[52,1647,1648],{"class":58},"();",[52,1650,1651],{"class":451},"                             \u002F\u002F record (class-based) projection\n",[15,1653,1654],{},"Projections are not managed entities; they are read-only and don't add load to the persistence context. They should be the first choice for list screens and reports.",[10,1656,1658],{"id":1657},"_8-the-pagination-collection-fetch-trap","8. The pagination + collection fetch trap",[15,1660,1661,1662,1665,1666,1669,1670,1673],{},"When ",[27,1663,1664],{},"JOIN FETCH"," is combined with ",[27,1667,1668],{},"Pageable",", Hibernate can't apply the LIMIT in SQL (the join multiplies the rows). Instead it ",[19,1671,1672],{},"loads the whole result into memory and paginates there",", logging a warning:",[43,1675,1678],{"className":1676,"code":1677,"language":619,"meta":48},[617],"HHH90003004: firstResult\u002FmaxResults specified with collection fetch; applying in memory\n",[27,1679,1677],{"__ignoreMap":48},[15,1681,1682],{},"Make sure this doesn't slip by silently by turning it into an error:",[43,1684,1686],{"className":571,"code":1685,"language":573,"meta":48,"style":48},"spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true\n",[27,1687,1688],{"__ignoreMap":48},[52,1689,1690,1693,1695],{"class":54,"line":55},[52,1691,1692],{"class":580},"spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch",[52,1694,182],{"class":58},[52,1696,606],{"class":106},[15,1698,1699],{},"The right fix is two queries: first paginate only the ids, then fetch the associations for those ids:",[43,1701,1703],{"className":45,"code":1702,"language":47,"meta":48,"style":48},"@Query(\"select p.id from Post p order by p.createdAt desc\")\nPage\u003CLong> findPageIds(Pageable pageable);\n\n@Query(\"select p from Post p left join fetch p.comments where p.id in :ids order by p.createdAt desc\")\nList\u003CPost> findAllWithCommentsByIdIn(List\u003CLong> ids);\n",[27,1704,1705,1722,1745,1749,1766],{"__ignoreMap":48},[52,1706,1707,1709,1711,1713,1715,1718,1720],{"class":54,"line":55},[52,1708,59],{"class":58},[52,1710,874],{"class":62},[52,1712,141],{"class":58},[52,1714,157],{"class":58},[52,1716,1717],{"class":153},"select p.id from Post p order by p.createdAt desc",[52,1719,157],{"class":58},[52,1721,160],{"class":58},[52,1723,1724,1727,1729,1731,1733,1736,1738,1740,1743],{"class":54,"line":66},[52,1725,1726],{"class":62},"Page",[52,1728,171],{"class":58},[52,1730,1096],{"class":106},[52,1732,176],{"class":58},[52,1734,1735],{"class":336}," findPageIds",[52,1737,141],{"class":58},[52,1739,1668],{"class":62},[52,1741,1742],{"class":106}," pageable",[52,1744,810],{"class":58},[52,1746,1747],{"class":54,"line":82},[52,1748,130],{"emptyLinePlaceholder":129},[52,1750,1751,1753,1755,1757,1759,1762,1764],{"class":54,"line":97},[52,1752,59],{"class":58},[52,1754,874],{"class":62},[52,1756,141],{"class":58},[52,1758,157],{"class":58},[52,1760,1761],{"class":153},"select p from Post p left join fetch p.comments where p.id in :ids order by p.createdAt desc",[52,1763,157],{"class":58},[52,1765,160],{"class":58},[52,1767,1768,1770,1772,1774,1776,1779,1781,1783,1785,1787,1789,1792],{"class":54,"line":113},[52,1769,317],{"class":62},[52,1771,171],{"class":58},[52,1773,29],{"class":106},[52,1775,176],{"class":58},[52,1777,1778],{"class":336}," findAllWithCommentsByIdIn",[52,1780,141],{"class":58},[52,1782,317],{"class":62},[52,1784,171],{"class":58},[52,1786,1096],{"class":106},[52,1788,176],{"class":58},[52,1790,1791],{"class":106}," ids",[52,1793,810],{"class":58},[43,1795,1797],{"className":45,"code":1796,"language":47,"meta":48,"style":48},"Page\u003CLong> ids = postRepository.findPageIds(PageRequest.of(0, 20));\nList\u003CPost> posts = postRepository.findAllWithCommentsByIdIn(ids.getContent());\nreturn new PageImpl\u003C>(posts, ids.getPageable(), ids.getTotalElements());\n",[27,1798,1799,1844,1877],{"__ignoreMap":48},[52,1800,1801,1803,1805,1807,1809,1812,1814,1816,1818,1821,1823,1826,1828,1831,1833,1836,1838,1841],{"class":54,"line":55},[52,1802,1726],{"class":62},[52,1804,171],{"class":58},[52,1806,1096],{"class":62},[52,1808,176],{"class":58},[52,1810,1811],{"class":106}," ids ",[52,1813,182],{"class":58},[52,1815,331],{"class":106},[52,1817,282],{"class":58},[52,1819,1820],{"class":336},"findPageIds",[52,1822,141],{"class":58},[52,1824,1825],{"class":106},"PageRequest",[52,1827,282],{"class":58},[52,1829,1830],{"class":336},"of",[52,1832,141],{"class":58},[52,1834,1835],{"class":438},"0",[52,1837,857],{"class":58},[52,1839,1840],{"class":438}," 20",[52,1842,1843],{"class":58},"));\n",[52,1845,1846,1848,1850,1852,1854,1856,1858,1860,1862,1865,1867,1870,1872,1875],{"class":54,"line":66},[52,1847,317],{"class":62},[52,1849,171],{"class":58},[52,1851,29],{"class":62},[52,1853,176],{"class":58},[52,1855,326],{"class":106},[52,1857,182],{"class":58},[52,1859,331],{"class":106},[52,1861,282],{"class":58},[52,1863,1864],{"class":336},"findAllWithCommentsByIdIn",[52,1866,141],{"class":58},[52,1868,1869],{"class":106},"ids",[52,1871,282],{"class":58},[52,1873,1874],{"class":336},"getContent",[52,1876,419],{"class":58},[52,1878,1879,1882,1884,1887,1890,1893,1895,1897,1899,1902,1905,1907,1909,1912],{"class":54,"line":82},[52,1880,1881],{"class":185},"return",[52,1883,186],{"class":185},[52,1885,1886],{"class":62}," PageImpl",[52,1888,1889],{"class":58},"\u003C>(",[52,1891,1892],{"class":106},"posts",[52,1894,857],{"class":58},[52,1896,1791],{"class":106},[52,1898,282],{"class":58},[52,1900,1901],{"class":336},"getPageable",[52,1903,1904],{"class":58},"(),",[52,1906,1791],{"class":106},[52,1908,282],{"class":58},[52,1910,1911],{"class":336},"getTotalElements",[52,1913,419],{"class":58},[10,1915,1917],{"id":1916},"_9-multiplebagfetchexception","9. MultipleBagFetchException",[15,1919,1920,1921,1923],{},"If you try to fetch two ",[27,1922,317],{}," collections in the same query:",[43,1925,1927],{"className":45,"code":1926,"language":47,"meta":48,"style":48},"@Query(\"select p from Post p left join fetch p.comments left join fetch p.tags\")\nList\u003CPost> findAllWithEverything();\n\u002F\u002F MultipleBagFetchException: cannot simultaneously fetch multiple bags\n",[27,1928,1929,1946,1961],{"__ignoreMap":48},[52,1930,1931,1933,1935,1937,1939,1942,1944],{"class":54,"line":55},[52,1932,59],{"class":58},[52,1934,874],{"class":62},[52,1936,141],{"class":58},[52,1938,157],{"class":58},[52,1940,1941],{"class":153},"select p from Post p left join fetch p.comments left join fetch p.tags",[52,1943,157],{"class":58},[52,1945,160],{"class":58},[52,1947,1948,1950,1952,1954,1956,1959],{"class":54,"line":66},[52,1949,317],{"class":62},[52,1951,171],{"class":58},[52,1953,29],{"class":106},[52,1955,176],{"class":58},[52,1957,1958],{"class":336}," findAllWithEverything",[52,1960,340],{"class":58},[52,1962,1963],{"class":54,"line":82},[52,1964,1965],{"class":451},"\u002F\u002F MultipleBagFetchException: cannot simultaneously fetch multiple bags\n",[15,1967,1968,1969,1971],{},"The Cartesian product of ",[27,1970,317],{}," (unordered, duplicate-allowing \"bag\") collections can't be split back correctly. Fixes:",[1973,1974,1975,1983],"ul",{},[1976,1977,1978,1979,1982],"li",{},"Turn one or both collections into a ",[27,1980,1981],{},"Set",". The error goes away but the Cartesian product stays: 100 comments × 10 tags = 1,000 rows per post.",[1976,1984,1985,1986,1988],{},"Better: separate queries. Within the same transaction the second query fills the same ",[27,1987,29],{}," instances in the persistence context:",[43,1990,1992],{"className":45,"code":1991,"language":47,"meta":48,"style":48},"List\u003CPost> posts = em.createQuery(\n        \"select p from Post p left join fetch p.comments where p.id in :ids\", Post.class)\n    .setParameter(\"ids\", ids).getResultList();\n\nem.createQuery(\n        \"select p from Post p left join fetch p.tags where p in :posts\", Post.class)\n    .setParameter(\"posts\", posts).getResultList();\n",[27,1993,1994,2019,2039,2066,2070,2081,2100],{"__ignoreMap":48},[52,1995,1996,1998,2000,2002,2004,2006,2008,2011,2013,2016],{"class":54,"line":55},[52,1997,317],{"class":62},[52,1999,171],{"class":58},[52,2001,29],{"class":62},[52,2003,176],{"class":58},[52,2005,326],{"class":106},[52,2007,182],{"class":58},[52,2009,2010],{"class":106}," em",[52,2012,282],{"class":58},[52,2014,2015],{"class":336},"createQuery",[52,2017,2018],{"class":58},"(\n",[52,2020,2021,2024,2027,2029,2031,2033,2035,2037],{"class":54,"line":66},[52,2022,2023],{"class":58},"        \"",[52,2025,2026],{"class":153},"select p from Post p left join fetch p.comments where p.id in :ids",[52,2028,157],{"class":58},[52,2030,857],{"class":58},[52,2032,76],{"class":106},[52,2034,282],{"class":58},[52,2036,655],{"class":106},[52,2038,160],{"class":58},[52,2040,2041,2044,2047,2049,2051,2053,2055,2057,2059,2061,2064],{"class":54,"line":82},[52,2042,2043],{"class":58},"    .",[52,2045,2046],{"class":336},"setParameter",[52,2048,141],{"class":58},[52,2050,157],{"class":58},[52,2052,1869],{"class":153},[52,2054,157],{"class":58},[52,2056,857],{"class":58},[52,2058,1791],{"class":106},[52,2060,745],{"class":58},[52,2062,2063],{"class":336},"getResultList",[52,2065,340],{"class":58},[52,2067,2068],{"class":54,"line":97},[52,2069,130],{"emptyLinePlaceholder":129},[52,2071,2072,2075,2077,2079],{"class":54,"line":113},[52,2073,2074],{"class":106},"em",[52,2076,282],{"class":58},[52,2078,2015],{"class":336},[52,2080,2018],{"class":58},[52,2082,2083,2085,2088,2090,2092,2094,2096,2098],{"class":54,"line":126},[52,2084,2023],{"class":58},[52,2086,2087],{"class":153},"select p from Post p left join fetch p.tags where p in :posts",[52,2089,157],{"class":58},[52,2091,857],{"class":58},[52,2093,76],{"class":106},[52,2095,282],{"class":58},[52,2097,655],{"class":106},[52,2099,160],{"class":58},[52,2101,2102,2104,2106,2108,2110,2112,2114,2116,2118,2120,2122],{"class":54,"line":133},[52,2103,2043],{"class":58},[52,2105,2046],{"class":336},[52,2107,141],{"class":58},[52,2109,157],{"class":58},[52,2111,1892],{"class":153},[52,2113,157],{"class":58},[52,2115,857],{"class":58},[52,2117,359],{"class":106},[52,2119,745],{"class":58},[52,2121,2063],{"class":336},[52,2123,340],{"class":58},[10,2125,2127],{"id":2126},"_10-eager-is-not-a-fix","10. EAGER is not a fix",[15,2129,2130,2133],{},[27,2131,2132],{},"fetch = FetchType.EAGER"," looks like it solves the problem at first, but:",[1973,2135,2136,2143,2146],{},[1976,2137,2138,2139,2142],{},"The association is loaded on ",[19,2140,2141],{},"every"," query, even when it isn't needed.",[1976,2144,2145],{},"In JPQL queries, EAGER associations are often still loaded with separate queries; the N+1 doesn't go away, it just becomes invisible.",[1976,2147,2148],{},"It's hard to undo later, because the rest of the code assumes the association is always populated.",[15,2150,2151,2153,2154,2157,2158,2161],{},[27,2152,553],{}," and ",[27,2155,2156],{},"@OneToOne"," are EAGER by default. The rule is simple: ",[19,2159,2160],{},"make every association LAZY"," and fetch per query where you need it.",[43,2163,2165],{"className":45,"code":2164,"language":47,"meta":48,"style":48},"@ManyToOne(fetch = FetchType.LAZY)\nprivate Author author;\n",[27,2166,2167,2187],{"__ignoreMap":48},[52,2168,2169,2171,2173,2175,2177,2179,2181,2183,2185],{"class":54,"line":55},[52,2170,59],{"class":58},[52,2172,269],{"class":62},[52,2174,141],{"class":58},[52,2176,274],{"class":75},[52,2178,147],{"class":58},[52,2180,279],{"class":106},[52,2182,282],{"class":58},[52,2184,285],{"class":106},[52,2186,160],{"class":58},[52,2188,2189,2191,2194,2197],{"class":54,"line":66},[52,2190,1169],{"class":62},[52,2192,2193],{"class":62}," Author",[52,2195,2196],{"class":106}," author",[52,2198,110],{"class":58},[10,2200,2202],{"id":2201},"_11-open-in-view-hides-the-problem","11. Open-in-view hides the problem",[15,2204,2205,2206,2209,2210,2213,2214,2217],{},"In Spring Boot, ",[27,2207,2208],{},"spring.jpa.open-in-view"," is ",[27,2211,2212],{},"true"," by default. It keeps the session open for the whole HTTP request, so accessing lazy associations in a controller or during JSON serialization doesn't fail — it ",[19,2215,2216],{},"silently issues new queries",". Many N+1s don't happen in the service layer but while Jackson serializes an entity.",[43,2219,2221],{"className":571,"code":2220,"language":573,"meta":48,"style":48},"spring.jpa.open-in-view=false\n",[27,2222,2223],{"__ignoreMap":48},[52,2224,2225,2227,2229],{"class":54,"line":55},[52,2226,2208],{"class":580},[52,2228,182],{"class":58},[52,2230,2231],{"class":106},"false\n",[15,2233,2234,2235,2238],{},"Once it's off, lazy access outside a transaction throws ",[27,2236,2237],{},"LazyInitializationException",". That's not a bug; it's a signal to state explicitly which data is loaded where.",[10,2240,2242],{"id":2241},"_12-which-technique-when","12. Which technique when?",[2244,2245,2246,2265],"table",{},[2247,2248,2249],"thead",{},[2250,2251,2252,2256,2259,2262],"tr",{},[2253,2254,2255],"th",{},"Technique",[2253,2257,2258],{},"Query count",[2253,2260,2261],{},"When?",[2253,2263,2264],{},"Watch out",[2266,2267,2268,2283,2299,2318,2333,2346],"tbody",{},[2250,2269,2270,2275,2277,2280],{},[2271,2272,2273],"td",{},[27,2274,1664],{},[2271,2276,545],{},[2271,2278,2279],{},"A single association, no pagination",[2271,2281,2282],{},"In-memory paging with pagination",[2250,2284,2285,2289,2291,2294],{},[2271,2286,2287],{},[27,2288,1109],{},[2271,2290,545],{},[2271,2292,2293],{},"Spring Data derived methods",[2271,2295,2296,2297],{},"Same limits as ",[27,2298,1664],{},[2250,2300,2301,2309,2312,2315],{},[2271,2302,2303,2306,2307],{},[27,2304,2305],{},"@BatchSize"," \u002F ",[27,2308,1306],{},[2271,2310,2311],{},"1 + N\u002Fbatch",[2271,2313,2314],{},"General safety net, paged lists",[2271,2316,2317],{},"Doesn't get down to one query",[2250,2319,2320,2325,2327,2330],{},[2271,2321,2322],{},[27,2323,2324],{},"FetchMode.SUBSELECT",[2271,2326,1252],{},[2271,2328,2329],{},"All collections always needed",[2271,2331,2332],{},"First query runs again",[2250,2334,2335,2338,2340,2343],{},[2271,2336,2337],{},"DTO \u002F record projection",[2271,2339,545],{},[2271,2341,2342],{},"Lists, reports, API responses",[2271,2344,2345],{},"Not an entity, can't be updated",[2250,2347,2348,2351,2354,2357],{},[2271,2349,2350],{},"Two-step query",[2271,2352,2353],{},"2–3",[2271,2355,2356],{},"Pagination + collection",[2271,2358,2359],{},"A bit more code",[10,2361,2363],{"id":2362},"conclusion","Conclusion",[15,2365,2366,2367,2369,2370,2373,2374,1249,2376,2378],{},"The N+1 problem isn't a Hibernate bug; it's the natural consequence of lazy loading. The approach that works well in practice: declare every association LAZY, add a safety net with ",[27,2368,1306],{},", turn off ",[27,2371,2372],{},"open-in-view",", load the data each screen needs explicitly with ",[27,2375,1664],{},[27,2377,1109],{}," or projections, and verify query counts in tests for critical flows.",[2380,2381,2382],"style",{},"html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}",{"title":48,"searchDepth":66,"depth":66,"links":2384},[2385,2386,2387,2388,2389,2390,2391,2392,2393,2394,2395,2396,2397,2398],{"id":12,"depth":66,"text":13},{"id":37,"depth":66,"text":38},{"id":564,"depth":66,"text":565},{"id":825,"depth":66,"text":826},{"id":954,"depth":66,"text":955},{"id":1113,"depth":66,"text":1114},{"id":1310,"depth":66,"text":1311},{"id":1428,"depth":66,"text":1429},{"id":1657,"depth":66,"text":1658},{"id":1916,"depth":66,"text":1917},{"id":2126,"depth":66,"text":2127},{"id":2201,"depth":66,"text":2202},{"id":2241,"depth":66,"text":2242},{"id":2362,"depth":66,"text":2363},"2026-11-21","How to detect the N+1 query problem and fix it with JOIN FETCH, @EntityGraph, @BatchSize, SUBSELECT and DTO projections — a practical guide with examples.","md",{},"\u002Fen\u002Fblog\u002Fhibernate-n-plus-one-problem",{"title":5,"description":2400},"en\u002Fblog\u002Fhibernate-n-plus-one-problem",[47,2407,2408,2409,2410],"hibernate","jpa","database","guide","\u002Fblog\u002Fhibernate-n-plus-one-problem","uWoglOorlUdXJ5BgWGaKhpdSjgx1K6coAbId5ysOhd4",[2414,2420,2428],{"path":2415,"title":2416,"description":2417,"date":2418,"tags":2419},"\u002Fen\u002Fblog\u002Fjpa-entity-mapping-best-practices","JPA and Hibernate Entity Mapping Best Practices","What to watch for when designing entities with Hibernate 7 and Spring Boot — ID strategies and batching, LAZY associations, bidirectional mappings, equals\u002FhashCode, @Version, auditing and schema management.","2026-11-20",[47,2407,2408,2409,2410],{"path":2421,"title":2422,"description":2423,"date":2424,"tags":2425},"\u002Fen\u002Fblog\u002Fspring-boot-virtual-threads","Virtual Threads in Spring Boot with Java 25","Enabling virtual threads in Spring Boot, how Java 24+ fixed the pinning problem, the connection-pool bottleneck, scoped values, structured concurrency and observability tips.","2026-11-19",[47,2426,2427,2410],"spring","concurrency",{"path":2429,"title":2430,"description":2431,"date":2432,"tags":2433},"\u002Fen\u002Fblog\u002Fspring-boot-4-whats-new","Spring Boot 4 and Spring Framework 7: What's New and How to Migrate","The main changes in Spring Boot 4 and Spring Framework 7 — Jakarta EE 11, modular auto-configuration, JSpecify null-safety, API versioning, HTTP service clients, built-in resilience, Jackson 3 and the steps to migrate from 3.5.","2026-11-18",[47,2426,2410],1795226875731]