{"id":2296,"date":"2012-02-21T21:44:24","date_gmt":"2012-02-21T20:44:24","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=2296"},"modified":"2021-08-24T13:27:01","modified_gmt":"2021-08-24T12:27:01","slug":"modify-annotations-in-scala","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2012\/02\/modify-annotations-in-scala\/","title":{"rendered":"Modify Annotations in Scala"},"content":{"rendered":"<div id=\"attachment_2299\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/02\/ScalaAnnotations.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2299\" class=\"size-medium wp-image-2299\" title=\"ScalaAnnotations\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/02\/ScalaAnnotations-300x193.png\" alt=\"Best Organized Kitchen Ever?\" width=\"300\" height=\"193\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/02\/ScalaAnnotations-300x193.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/02\/ScalaAnnotations-1024x658.png 1024w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/02\/ScalaAnnotations.png 1200w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-2299\" class=\"wp-caption-text\">Best Organized Kitchen Ever?<\/p><\/div>\n<p>In Scala I rarely use annotations. However when you need to interoperate with a framework which relies on annotations you need use them. So lately I wanted to store some simple case classes with JPA. As you know, JPA relies quite heavily on annotations, unless you move everything out to XML descriptors.<\/p>\n<p>I wanted to store something like this:<\/p>\n<script src=\"https:\/\/gist.github.com\/1878790.js?file=MyStoredData.scala\"><\/script><noscript><pre><code class=\"language-scala scala\">@Entity\ncase class MyStoredData(userName:String,\n                         realName:String,\n                         @Id id:Long =0) {\n  \/\/ JPA requires a parameter-free constructor\n  def this() = this(&quot;&quot;,&quot;&quot;)\n}\n<\/code><\/pre><\/noscript>\n<p>Unfortunately this results in exceptions like the one below. Basically the annotation is not placed on the \u2018field\u2019 but rather on the parameter of the constructor.<\/p>\n<script src=\"https:\/\/gist.github.com\/1878790.js?file=Exception.txt\"><\/script><noscript><pre><code class=\"language-text text\">Caused by: com.versant.jpa.enhancer.exceptions.MissingIdException: missing ID annotation or mapping for info.gamlor.vjpa.model.MyStoredData\n\tat com.versant.jpa.enhancer.JpaClassEnhancer.enhance(JpaClassEnhancer.java:123)\n\tat com.versant.jpa.enhancer.JpaClassEnhancer.enhance(JpaClassEnhancer.java:50)\n\tat com.versant.jpa.enhancer.JpaClassTransformer.transform(JpaClassTransformer.java:51)\n\tat sun.instrument.TransformerManager.transform(TransformerManager.java:169)\n\tat sun.instrument.InstrumentationImpl.transform(InstrumentationImpl.java:365)<\/code><\/pre><\/noscript>\n<p>Now you actually can specify on which elements such an annotation is added. So in this situation we can tell the Scala to ensure that the annotation is also placed on the id field:<\/p>\n<script src=\"https:\/\/gist.github.com\/1878790.js?file=FixedAnnotation.scala\"><\/script><noscript><pre><code class=\"language-scala scala\">@Entity\ncase class MyStoredData(userName:String,\n                         realName:String,\n                         \/\/ ensure that the field is also annotated\n                         @(Id @field) id:Long =0) {\n  \/\/ JPA requires a parameter-free constructor\n  def this() = this(&quot;&quot;,&quot;&quot;)\n}\n<\/code><\/pre><\/noscript>\n<p>This will fix the issue. There are more possible targets, like Scala accessors and Java Beans getter\/setters for @BeanProperty annotated fields. Take a look at <a href=\"https:\/\/www.gamlor.info\/@BeanProperty\">the documentation here<\/a>.<\/p>\n<p>That\u2019s all. Nothing more to tell =)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Scala I rarely use annotations. However when you need to interoperate with a framework which relies on annotations you need use them. So lately I wanted to store some&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[15,17],"tags":[226],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2296"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2296"}],"version-history":[{"count":6,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2296\/revisions"}],"predecessor-version":[{"id":3903,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2296\/revisions\/3903"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}