As an object-oriented developer, we want to avoid having larger classes with tons of unrelatable fields. And so, we might often feel the need to represent a JPA Entity using multiple objects.
In this quick tutorial, we’ll learn how to achieve it using @Embedded and @Embeddable annotations in JPA or Hibernate.
Let’s say we have a person table with the following columns:
id|firstName|middleName|lastName|street|city|country|pincode
Mapping so many properties directly in our entity class doesn’t feel that natural to our developer instinct. Also, changing the database table structure is not a viable option. What do we do?
Let’s define a PersonName and Address classes first:
@Embeddable public class PersonName { private String firstName; private String middleName; private String lastName; //constructors, getters, setters ... } @Embeddable public class Address { private String street; private String city; private String country; private String pincode; //constructors, getters, setters ... }
Finally, we’ll use @Embedded annotation to embed a specific type.
Let’s define our Person entity that’ll represent our person table:
@Entity public class Person { @Id @GeneratedValue private Integer id; @Embedded private PersonName name; @Embedded private Address address; //constructor, getters, setters ... }
Embeddable objects often come handy specifically when we have multiple entities using it.
Let’s now say, we have another table – office:
id|streetAddr|city|country|postcode|...
Here as well, we can use the same Address embeddable object. The idea is to override the street and pincode attributes of the Address class using @AttributeOverrides and @AttributeOverride annotations:
@Entity public class Office { @Id @GeneratedValue private Integer id; @Embedded @AttributeOverrides(value = { @AttributeOverride(name = "street", column = @Column(name = "streetAddr")), @AttributeOverride(name = "pincode", column = @Column(name = "postcode")) }) private Address address; ... }
Rather, we can override any of the column properties of our embeddable type.
In this tutorial, we explored JPA @Emdeddable and @Embedded annotations.
Concise and to the point explanation…
Great!
Thanks a lot for your work.
Impressive presentation in clean and crispy way!!!
Absolutely fantastic article!
Basically I have developed interests in front-end and back-end both. Just because of that I put myself in troubles and create problems myself which do not easily let me reach to the solutions :P. This time I was again planning such a complex scenario for a project but your article as provided me a clue upfront before I would again get a nightmare.
On a side note; My website that I have added here has nothing much except a starter template I chose when I deployed it on the Netlify platform. The very reason behind this was to start my blog and venture into affiliate business. Till date I have not taken any effective step in that direction. Your article as again inspired me to finally do something for that dream.
Thanks a lot and keep doing the best like this! 🙂