{"id":1411,"date":"2011-07-19T20:54:57","date_gmt":"2011-07-19T20:54:57","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1411"},"modified":"2011-07-19T20:56:01","modified_gmt":"2011-07-19T20:56:01","slug":"jpa-accessing-the-primary-key-id-allocated-during-a-persist-operation-on-a-new-entity","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1411","title":{"rendered":"JPA &#8211; Accessing the primary key ID allocated during a persist operation on a new entity"},"content":{"rendered":"<p>I wanted to do this in order to use the ID as part of the creation of a Base64 encoded tree path in another column in the same row, as I was using path enumeration as my tree algorithm (see <a href=\"http:\/\/www.slideshare.net\/billkarwin\/sql-antipatterns-strike-back\"><strong>here<\/strong><\/a> on slideshare.net for an excellent article on database tree algorithms).<\/p>\n<p><em>The simple answer is \u2013 you can\u2019t do this reliably, unless you allocate IDs yourself in some way.<\/em><\/p>\n<ol>\n<li>The ID is not available before a persist. I could call flush and then read it back, create the TreePath and then persist again &#8211; this is safe. This is discussed <a href=\"http:\/\/stackoverflow.com\/questions\/6608993\/java-jpa-eclipselink-how-to-receive-the-next-generatedvalue-before-persisting-a\"><strong>here<\/strong><\/a> on Stack Overflow.<\/li>\n<li>The JPA Lifecycle callback @PrePersist does not guarantee that the ID is visible. The net is rather quiet on this but this post <a href=\"https:\/\/forum.hibernate.org\/viewtopic.php?f=9&amp;t=971253\"><strong>here<\/strong><\/a> about hibernate says it cannot be relied upon\u00a0 (@PostPersist would of course be different). There are strong limits on what you can do in such a callback or entity listener. For example, you can&#8217;t do entity manager operations, persist entities, or refer to other entities etc.<\/li>\n<li>Triggers would be another way to avoid the double update. I could set the rest of my TreePath prior to the first persist, and then retrieve the actual ID in a before insert trigger using the <em>new.TreePathID<\/em> syntax (MySQL and Oracle use similar syntax in this respect). I could then encode it in Base64 using a stored procedure, and append it to the treepath. Oracle has a built in package with Base64 encoding and decoding available (the <em>utl_encode<\/em> package). For MySql there is an open source example on the internet <strong><a href=\"http:\/\/wi-fizzle.com\/downloads\/base64.sql\">here<\/a>. <\/strong>From posts on the net, e.g. <a href=\"http:\/\/www.roseindia.net\/software-tutorials\/detail\/23892\"><strong>here<\/strong><\/a>, triggers do work in MySQL via JPA.<\/li>\n<\/ol>\n<p>The best solution looks like using triggers on the database server, as this avoids the double update. I have yet to investigate\/try this.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to do this in order to use the ID as part of the creation of a Base64 encoded tree path in another column in the same row, as I was using path enumeration as my tree algorithm (see here on slideshare.net for an excellent article on database tree algorithms). The simple answer is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[35,40,182,16,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1411"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1411"}],"version-history":[{"count":1,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1411\/revisions"}],"predecessor-version":[{"id":1412,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1411\/revisions\/1412"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}