{"id":2089485,"date":"2022-01-27T10:03:00","date_gmt":"2022-01-27T08:03:00","guid":{"rendered":"http:\/\/nhub.news\/?p=2089485"},"modified":"2022-01-27T12:03:27","modified_gmt":"2022-01-27T10:03:27","slug":"inheritance-vs-composition-in-jpa","status":"publish","type":"post","link":"http:\/\/nhub.news\/de\/2022\/01\/inheritance-vs-composition-in-jpa\/","title":{"rendered":"Inheritance vs. Composition in JPA"},"content":{"rendered":"<p style=\"text-align: justify;\"><b>There are two approaches: Inheritance and Composition with their pros and cons. Let&#8217;s see what they are on the not quite &#8222;real-world&#8220; but representative example<\/b><br \/>\nJoin the DZone community and get the full member experience. \u00abDon&#8217;t repeat yourself\u00bb or \u00abDRY\u00bb. Developers try to adhere to this principle during software development. It helps to avoid redundant code writing and, as a result, simplifies its maintainability in the future. But how to achieve this principle in the JPA world? There are two approaches: Inheritance and Composition. Both have their pros and cons. Let&#8217;s figure out what they are on the not quite &#8222;real-world&#8220; but representative example. Our model has three entities: Article, Author, and Spectator. Each entity has fields for audit (createdDate, createdBy, modifiedDate, and modifedBy). Author and Spectator also have fields for address (country, city, street, building). To comply with the DRY principle, let&#8217;s take duplicate fields to the separate Mapped Superclasses. We will inherit our entities from them. Since all entities must have fields for auditing, let&#8217;s start with the BaseEntityAudit class. We will create a &#8222;BaseEntityAuditAddress&#8220; class for entities with address fields and inherit it from the BaseEntityAudit class. NOTE: All the approaches presented in this article are implemented and available in this repository on GitHub. The hierarchy of entities is implemented so that we no longer repeat ourselves. Mission complete. But what if&#8230;? But what if the initial requirements for our model change a little? For example, consider that Article needs just audit fields, Spectator needs address fields only, and Author needs both. In this case, following the Inheritance strategy, we will have to neglect the DRY principle anyway because there are no multiple inheritances for classes in Java. In other words, our hierarchy will look like a diagram below, which is impossible to implement in Java. We will have to leave two Superclasses created earlier and one with address fields only for Spectator. So, the address fields will be repeated in two entities. If we want to comply with the DRY principle, let&#8217;s use the composition instead. Let\u2019s implement the composition via interfaces with only one method: getBaseEntityAudit() or getBaseEntityAddress(). As you can guess, they will return embeddable entities containing the corresponding fields. Implementing these methods in entities will replace getters for @Embedded fields. Now we are free to use those interfaces in any entity. To implement interface methods, you need to add an @Embedded attribute and a getter for it. We have achieved DRY in the entity code, but what about business code that works with these entities? Let&#8217;s imagine that we need a method that will return a list of countries from a list of entities. In our example with inheritance, we will need to pass a list with the BaseEntityAuditAddress type as a parameter. And we will be able to use this method for both Authors and Spectators. The usage will be the following: However, changing the approach will not change anything. All that needs to be changed is to replace BaseEntityAuditAddress with EntityWithAddressFields. Perhaps the method has become easier to read since we explicitly refer to an entity with only the address and not both the address and the audit fields. In the end, the composition seems to have more flexible use cases. But even if you decide to use inheritance (one of the possible reasons: to limit such flexibility intentionally), JPA Buddy will help you regardless of the chosen approach. Check it out in a short video version of this article. Published at DZone with permission of Georgii Vlasov. See the original article here. Opinions expressed by DZone contributors are their own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two approaches: Inheritance and Composition with their pros and cons. Let&#8217;s see what they are on the not quite &#8222;real-world&#8220; but representative example Join the DZone community and get the full member experience. \u00abDon&#8217;t repeat yourself\u00bb or \u00abDRY\u00bb. Developers try to adhere to this principle during software development. It helps to avoid redundant [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2089484,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[93],"tags":[],"_links":{"self":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/2089485"}],"collection":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/comments?post=2089485"}],"version-history":[{"count":0,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/posts\/2089485\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media\/2089484"}],"wp:attachment":[{"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/media?parent=2089485"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/categories?post=2089485"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/nhub.news\/de\/wp-json\/wp\/v2\/tags?post=2089485"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}