{"id":901,"date":"2015-03-30T14:16:57","date_gmt":"2015-03-30T18:16:57","guid":{"rendered":"http:\/\/shirishranjit.com\/blog1\/?page_id=901"},"modified":"2015-03-31T07:27:48","modified_gmt":"2015-03-31T11:27:48","slug":"java-persistence-entitymanager-persist-v-s-merge","status":"publish","type":"page","link":"https:\/\/shirishranjit.com\/blog1\/java-stuff\/java-persistence-entitymanager-persist-v-s-merge","title":{"rendered":"Java Persistence API &#8211; EntityManager"},"content":{"rendered":"<p>JPA is annotation based OR mapping. It is very powerful as the annotation binds a datastore to JAVA classes. These objects are datastore context aware. The JPA library manages the write\/update to the objects.<\/p>\n<p>The EntityManager class has many methods. There are two methods that concerns most of datastore operations and their usage are:<\/p>\n<p><strong>merge<\/strong>() &#8211; does a select to check if the entity exists so that it can do update. Any detached JPA entity must be merged. If persist() is used instead, then a new entity is created in the store.<\/p>\n<p><strong>persist<\/strong>() &#8211; deos not do select and does insert with generated ID. This is for persisting new object into a datastore.<\/p>\n<h2>Persistence Unit<\/h2>\n<p>In an application, persistable objects can be independently grouped so that they can be treated separately. They may be grouped within a different DAO object. JPA introduces the idea of a persistence-unit. A persistence-unit provides a convenient way of specifying a set of metadata files, and classes, and jars that contain all classes to be persisted in a grouping. The persistence-unit is named, and the name is used for identifying it.<\/p>\n<p><em>Persistence Unit<\/em> should be used so that a group of classes may be stored in different stores. This is very powerful way of designing an application that has lets say, structured (SQL), or documents (NoSQL) based data stores.<\/p>\n<h2>Reference<\/h2>\n<ul>\n<li><a href=\"http:\/\/stackoverflow.com\/questions\/1069992\/jpa-entitymanager-why-use-persist-over-merge\">http:\/\/stackoverflow.com\/questions\/1069992\/jpa-entitymanager-why-use-persist-over-merge<\/a><\/li>\n<li><a href=\"https:\/\/cloud.google.com\/appengine\/docs\/java\/datastore\/jpa\/overview\">https:\/\/cloud.google.com\/appengine\/docs\/java\/datastore\/jpa\/overview<\/a><\/li>\n<li><a href=\"http:\/\/www.datanucleus.org\/products\/datanucleus\/jpa\/persistence_unit.html\">http:\/\/www.datanucleus.org\/products\/datanucleus\/jpa\/persistence_unit.html<\/a><\/li>\n<li><a href=\"http:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/Persistence.html#createEntityManagerFactory(java.lang.String)\">http:\/\/docs.oracle.com\/javaee\/7\/api\/javax\/persistence\/Persistence.html#createEntityManagerFactory(java.lang.String)<\/a><\/li>\n<\/ul>\n<div class=\"twttr_buttons\"><div class=\"twttr_twitter\">\n\t\t\t\t\t<a href=\"http:\/\/twitter.com\/share?text=Java+Persistence+API+-+EntityManager\" class=\"twitter-share-button\" data-via=\"\" data-hashtags=\"\"  data-size=\"default\" data-url=\"https:\/\/shirishranjit.com\/blog1\/java-stuff\/java-persistence-entitymanager-persist-v-s-merge\"  data-related=\"\" target=\"_blank\">Tweet<\/a>\n\t\t\t\t<\/div><div class=\"twttr_followme\">\n\t\t\t\t\t\t<a href=\"https:\/\/twitter.com\/shiranjit\" class=\"twitter-follow-button\" data-size=\"default\"  data-show-screen-name=\"false\"  target=\"_blank\">Follow me<\/a>\n\t\t\t\t\t<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>JPA is annotation based OR mapping. It is very powerful as the annotation binds a datastore to JAVA classes. These objects are datastore context aware. The JPA library manages the write\/update to the objects. The EntityManager class has many methods. &hellip; <a href=\"https:\/\/shirishranjit.com\/blog1\/java-stuff\/java-persistence-entitymanager-persist-v-s-merge\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":524,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-901","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/901"}],"collection":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/comments?post=901"}],"version-history":[{"count":4,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/901\/revisions"}],"predecessor-version":[{"id":905,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/901\/revisions\/905"}],"up":[{"embeddable":true,"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/pages\/524"}],"wp:attachment":[{"href":"https:\/\/shirishranjit.com\/blog1\/wp-json\/wp\/v2\/media?parent=901"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}