{"id":209,"date":"2009-12-04T17:09:26","date_gmt":"2009-12-04T17:09:26","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=209"},"modified":"2009-12-09T18:15:58","modified_gmt":"2009-12-09T18:15:58","slug":"using-jpa-annotations-with-xml","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=209","title":{"rendered":"Using JPA Annotations with XML"},"content":{"rendered":"<p>It is possible to use both annotations and descriptors in orm.xml to define entity mappings with JPA. The debate on which way to go has become very heated, but in my opinion there is a strong case for both.<\/p>\n<ol>\n<li>Annotations are best for metadata which you would consider closely bound to the code. This promotes clarity, as elements which are closely bound together are also in close proximity, promoting clarity and maintainability.<\/li>\n<li>XML is best for metadata which may change independantly of the code, and provides the ability to isolate platform specific issues from the code, for example to enhance persistence provider independance.<\/li>\n<\/ol>\n<p>The OO design principle <em>&#8220;Separate what changes from what stays the same&#8221;<\/em> comes to mind clearly here.<\/p>\n<p>I found the\u00a0following simple example helpful in this context. The ID for\u00a0the entity is defined via annotations in the code. However, I have defined the ID generator seperately in orm.xml. The generator name is fixed and referred to by the @GeneratedValue annotation, but the actual generator is in orm.xml and may be specified either as a table generator (which is database independant), or as a sequence generator which allows me to take advantage of Oracle&#8217;s sequences for primary key generation. Either generator may be defined in orm.xml, but the code does not change. This gives database independance, whilst still allowing leverage of the enhanced features of a specific database platform. In the example below, for illustration, orm.xml contains both generators and one has been commented out.<\/p>\n<p><strong>Note<\/strong> that to enable sequence generation in Oracle for this example to work, the correct properties must be set in <strong>persistence.xml<\/strong>, as by default Eclipselink will use table based ID generation even if you turn sequence generation on. The way to do this is\u00a0detailed in <strong><a title=\"http:\/\/salientsoft.co.uk\/?p=295\" href=\"http:\/\/salientsoft.co.uk\/?p=295\">this post here<\/a><\/strong>.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Class UserInfo<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.jpatest.domain;\r\nimport java.io.Serializable;\r\nimport javax.persistence.*;\r\n\r\n@Entity\r\npublic class UserInfo implements Serializable {\r\n\r\n\u00a0@Id\r\n\u00a0@GeneratedValue(generator=\"UserID\")\r\n\u00a0private long userID;\r\n\r\n\u00a0private String userName;\r\n\u00a0private static final long serialVersionUID = 1L;\u00a0\r\n\r\n\u00a0public UserInfo() {\r\n\u00a0\u00a0super();\r\n\u00a0} \u00a0\r\n\u00a0public long getUserID() {\r\n\u00a0\u00a0\u00a0return this.userID;\r\n\u00a0}\r\n\u00a0public void setUserID(long userID) {\r\n\u00a0\u00a0this.userID = userID;\r\n\u00a0}\u00a0\u00a0\u00a0\r\n\u00a0public String getUserName() {\r\n\u00a0\u00a0\u00a0return this.userName;\r\n\u00a0}\r\n\u00a0public void setUserName(String userName) {\r\n\u00a0\u00a0this.userName = userName;\r\n\u00a0}\r\n}<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">orm.xml<\/span><\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;entity-mappings version=\"1.0\" xmlns=\"<a href=\"http:\/\/java.sun.com\/xml\/ns\/persistence\/orm\">http:\/\/java.sun.com\/xml\/ns\/persistence\/orm<\/a>\"\r\n\u00a0 xmlns:xsi=\"<a href=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\">http:\/\/www.w3.org\/2001\/XMLSchema-instance<\/a>\"\r\n\u00a0 xsi:schemaLocation=\"<a href=\"http:\/\/java.sun.com\/xml\/ns\/persistence\/orm\">http:\/\/java.sun.com\/xml\/ns\/persistence\/orm<\/a>\r\n\u00a0\u00a0\u00a0 <a href=\"http:\/\/java.sun.com\/xml\/ns\/persistence\/orm_1_0.xsd\">http:\/\/java.sun.com\/xml\/ns\/persistence\/orm_1_0.xsd<\/a>\"&gt;\r\n\r\n\u00a0&lt;!--&lt;table-generator name=\"UserID\" \/&gt;--&gt;\r\n\r\n\u00a0&lt;sequence-generator name=\"UserID\" sequence-name=\"UserID\"\/&gt;\r\n\r\n&lt;\/entity-mappings&gt;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>It is possible to use both annotations and descriptors in orm.xml to define entity mappings with JPA. The debate on which way to go has become very heated, but in my opinion there is a strong case for both. Annotations are best for metadata which you would consider closely bound to the code. This promotes [&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":[182,29,16],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/209"}],"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=209"}],"version-history":[{"count":6,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/209\/revisions\/214"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}