{"id":557,"date":"2017-06-28T00:49:49","date_gmt":"2017-06-28T01:19:49","guid":{"rendered":"http:\/\/www.mind4y.co\/itstuff\/2017\/06\/28\/jpa-namedentitygraph\/"},"modified":"2020-03-19T23:30:34","modified_gmt":"2020-03-20T00:00:34","slug":"jpa-namedentitygraph","status":"publish","type":"post","link":"https:\/\/ultering.com\/it4us\/?p=557","title":{"rendered":"JPA: @namedEntityGraph"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\n\r\n# jpa 2.1\r\n# This anotation allows you to change the behavior of a Lazy entity loading default, in the case the var <theAtributeName> is the atribute that\r\n# will behave like EAGUER when using a query with \tTypedQuery<slave>.setHint(\"javax.persistence.loadgraph\", eg);\r\n# \r\n#theAtributeName: \r\n...\r\n\r\nimport javax.persistence.NamedAttributeNode;\r\nimport javax.persistence.NamedEntityGraph;\r\n\r\n...\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\n@NamedEntityGraph(name=\"aNameToCallIt\",attributeNodes={@NamedAttributeNode(\"theAtributeName\")})\r\n@Entity\r\n@Table(name = \"nameoftheTable\")\r\npublic class ClassName implements Serializable{\r\n...\r\n<\/pre>\n<p>#Using the EntityGraph attribute (with criteria)<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">\r\nEntityGraph&lt;Slave&gt; eg = (EntityGraph&lt;Slave&gt;) getEntityManager().getEntityGraph(&quot;slave.counterTypes&quot;);\r\nCriteriaBuilder cb = getEntityManager().getCriteriaBuilder();\r\nCriteriaQuery&lt;Slave&gt; criteria = cb.createQuery(Slave.class);\r\nRoot&lt;Slave&gt; root = criteria.from(Slave.class);\r\nParameterExpression&lt;Long&gt; param = cb.parameter(Long.class);\r\ncriteria.select(root).where(cb.equal(root.get(&quot;id&quot;), param));\r\nTypedQuery&lt;Slave&gt; q = getEntityManager().createQuery(criteria);\r\nq.setParameter(param, id);\r\nq.setHint(&quot;javax.persistence.loadgraph&quot;, eg);\r\n<\/pre>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p># jpa 2.1 # This anotation allows you to change the behavior of a Lazy entity loading default, in the case the var is the atribute that # will behave like EAGUER when using a query with TypedQuery.setHint(&#8220;javax.persistence.loadgraph&#8221;, eg); # #theAtributeName: &#8230; import javax.persistence.NamedAttributeNode; import javax.persistence.NamedEntityGraph; &#8230; @NamedEntityGraph(name=&#8221;aNameToCallIt&#8221;,attributeNodes={@NamedAttributeNode(&#8220;theAtributeName&#8221;)}) @Entity @Table(name = &#8220;nameoftheTable&#8221;) public class ClassName &#8230; <a href=\"https:\/\/ultering.com\/it4us\/?p=557\" class=\"more-link\">Read More<span class=\"screen-reader-text\"> &#8220;JPA: @namedEntityGraph&#8221;<\/span> &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[17,12],"tags":[18,13,19,20],"class_list":["post-557","post","type-post","status-publish","format-standard","hentry","category-database","category-java","tag-database","tag-java","tag-jdbc","tag-jpa"],"_links":{"self":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/557","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=557"}],"version-history":[{"count":1,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":2655,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=\/wp\/v2\/posts\/557\/revisions\/2655"}],"wp:attachment":[{"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ultering.com\/it4us\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}