Saturday, August 20, 2016

HIBERNATE: Embedding Value (Non Entity) Objects into Entity


  1. An entity has a meaning of its own and can exist independently. 
  2. Value objects on the other hand do not have a meaning of their own. 
  3. Hence, if we have embedding a value object into an entity, then the fields of the value object shall receive a corresponding column in the relational schema, resulting in a single table despite a clear association.
Let us add a value object called Address to the User class.
This new entity will map to the shown schema. (Since each user has one address only)







How can this be realized in Hibernate?

Answer: Using @Embeddable and @Embedded annotations from standard JPA.


Here is the definition of the Address class that will be embedded inside UserDetails class.





And here is the definition of the UserDetails class


No comments:

Post a Comment