{"id":369,"date":"2016-03-31T22:34:21","date_gmt":"2016-03-31T20:34:21","guid":{"rendered":"http:\/\/h2916922.stratoserver.net:8084\/?p=369"},"modified":"2016-05-11T21:01:58","modified_gmt":"2016-05-11T19:01:58","slug":"jpaeclipselink-dont-forget-to-annotate-complex-fields-with-mutable","status":"publish","type":"post","link":"https:\/\/www.ivojonker.nl\/?p=369","title":{"rendered":"JPA\/Eclipselink, don&#8217;t forget to annotate complex fields with @Mutable"},"content":{"rendered":"<p>Note to self: Don&#8217;t forget to annotate complex fiels with the @Mutable annotation.<\/p>\n<p>&nbsp;<\/p>\n<p>It might sound noobish, but i&#8217;ve just spend the last 2 hours trying to find out why my jpa\/eclilpse link entity wasn\u00a0&#8216;t updating my &#8216;complex&#8217; (hashmap) field. At first, i figured it had something to do with the @Converter i was using to serialize\/deserialize the HashMap into a JSON string. But eventually it turned out, that the change-tracking mechanism only works for non-mutable\/basic fields. And thus, no updates were detected, as i was updating the hashmap, instead of replacing it.<\/p>\n<p>So\u00a0as it turns out (i should have read the manual ;)) the <a href=\"http:\/\/www.eclipse.org\/eclipselink\/documentation\/2.4\/jpa\/extensions\/a_mutable.htm\">@Mutable<\/a> annotation is there to use.<\/p>\n<pre class=\"lang:java decode:true \" title=\"The code\">@Entity\r\npublic class BasicEntity {\r\n\r\n\t@Id\r\n\t@GeneratedValue\r\n\tprivate int id;\r\n\t\r\n\t@JsonAdapter(BasicEntityTypeAdapter.class)\r\n\tprivate BasicEntityType type;\r\n\r\n\tprivate String location;\r\n\t\r\n\tprivate float x;\r\n\tprivate float y;\r\n\t\r\n\t\r\n\t@Convert(converter = JPAHashmapToStringConverter.class) \/*store as String\/json*\/\r\n\t@Column(length=4096)\r\n\t@Mutable\r\n\tprivate HashMap&lt;String, Object&gt; properties;\r\n}\r\n\r\n@Converter\r\npublic class JPAHashmapToStringConverter implements AttributeConverter&lt;HashMap&lt;String, Object&gt;,String&gt;{\r\n\r\n\t  private final static Gson gson = new Gson();\r\n\t  private final static Class hashMapClass = new HashMap&lt;String,Object&gt; ().getClass();\r\n\r\n\t  @Override\r\n\t  public String convertToDatabaseColumn(HashMap&lt;String, Object&gt; meta) {\r\n\t\t  if (meta==null)meta = new HashMap&lt;String,Object&gt;();\r\n\t      return gson.toJson(meta);\r\n\t  }\r\n\r\n\t  @Override\r\n\t  public HashMap&lt;String, Object&gt; convertToEntityAttribute(String dbData) {\r\n\t\t \r\n\t\t  return gson.fromJson(dbData,hashMapClass);\r\n\t  }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Note to self: Don&#8217;t forget to annotate complex fiels with the @Mutable annotation. &nbsp; It might sound noobish, but i&#8217;ve just spend the last 2 hours trying to find out [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-369","post","type-post","status-publish","format-standard","hentry","category-geen-categorie"],"_links":{"self":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=369"}],"version-history":[{"count":3,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions"}],"predecessor-version":[{"id":371,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=\/wp\/v2\/posts\/369\/revisions\/371"}],"wp:attachment":[{"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ivojonker.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}