{"id":532,"date":"2014-08-13T15:06:30","date_gmt":"2014-08-13T15:06:30","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=532"},"modified":"2019-04-06T13:28:01","modified_gmt":"2019-04-06T13:28:01","slug":"using-embedabble-and-embedded-in-hibernate","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/using-embedabble-and-embedded-in-hibernate\/","title":{"rendered":"Using Embedabble and Embedded in Hibernate"},"content":{"rendered":"<p>There are two types of objects in Hibernate<br \/>\n1. Value Object<br \/>\n2. Entities<\/p>\n<p><em>Value Objects<\/em> are the objects which can not stand alone. Take Address, for example. If you say address, people will ask whose address is this. So it can not stand alone.<br \/>\n<em>Entity Objects<\/em> are those who can stand alone like College and Student.<\/p>\n<p>So in case of value objects preferred way is to Embed them into an entity object. In the example below Address class does not derive the meaning of its own. So Address is Value Object and is Embeddable. Whereas UserDetails(Entities) could stand on its own and it could have Embedded Objects<\/p>\n<p><strong>Address.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n import javax.persistence.Embeddable;\r\n\r\n@Embeddable\r\npublic class Address {\r\n\tprivate String Street;\r\n\tprivate String Location;\r\n\tprivate String City;\r\n\t\r\n\tpublic String getStreet() {\r\n\t\treturn Street;\r\n\t}\r\n\tpublic void setStreet(String street) {\r\n\t\tStreet = street;\r\n\t}\r\n\tpublic String getLocation() {\r\n\t\treturn Location;\r\n\t}\r\n\tpublic void setLocation(String location) {\r\n\t\tLocation = location;\r\n\t}\r\n\tpublic String getCity() {\r\n\t\treturn City;\r\n\t}\r\n\tpublic void setCity(String city) {\r\n\t\tCity = city;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>UserDetails.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class UserDetails \r\n{\r\n @Embedded\r\n private Address address;\r\n .\r\n .\r\n .\r\n}\r\n<\/pre>\n<p>The above code explains how the Embedded and Embeddable annotations can be used in the code.<\/p>\n<p><strong>CreateUser.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n  Address objAddress = new Address();\r\n  objUserDetail.setAddress(objAddress);\r\n<\/pre>\n<p><strong>Attribute Override<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Embedded\r\n@AttributeOverride (name=&quot;Street&quot;,column=@Column(name=&quot;HOME_STREET&quot;))\r\nprivate Address addr;\r\n<\/pre>\n<p><strong>Multiple Attribute Overrides<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Embedded\r\n @AttributeOverrides({\r\n\t@AttributeOverride(name=&quot;doorNo&quot;, column=@Column(name=&quot;Office_DoorNo&quot;)),\r\n\t@AttributeOverride(name=&quot;streetName&quot;, column=@Column(name=&quot;Office_streetName&quot;)),\r\n\t@AttributeOverride(name=&quot;location&quot;, column=@Column(name=&quot;Office_location&quot;)),\r\n\t@AttributeOverride(name=&quot;pincode&quot;, column=@Column(name=&quot;Office_pincode&quot;))\r\n})\r\nprivate Address addr;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There are two types of objects in Hibernate 1. Value Object 2. Entities Value Objects are the objects which can not stand alone. Take Address, for example. If you say address, people will ask whose address is this. So it can not stand alone. Entity Objects are those who can stand alone like College and&hellip; <a href=\"https:\/\/codethataint.com\/blog\/using-embedabble-and-embedded-in-hibernate\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-532","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/532","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":7,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"predecessor-version":[{"id":3291,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/532\/revisions\/3291"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}