Attribute Overriding and Primary Key as Embedded Object

Attribute Overriding and Embedded Object Keys

In the previous tutorial we have discussed the answers of these questions.

  • What is the difference between Entity and Value objects?
  • How we will store embedded objects in the database with hibernate?
  • How to create Embedded objects with Hibernate Annotations?

Answer to all these questions can be seen in this tutorial “Value Types and Embedding Objects

We have also seen an example to understand the answers of these questions. In that example we have stored an embedded object in the database the name of the object is Address and the class in which we have embed it is named as UserDetails class.  User details has id and name as fields and address has Street, City , Distinct and Country as fields. After embedding the object these all (id, name , Street, City , Distinct and Country)fields have been stored as one table.  Now there is a possibility that we have to use custom column names for these fields. We have @Column annotation which can be used for Id and name. Now the question arises “How do we give custom column names for embedded objects?”  We can do this by using the @Column annotation directly in the Address class.


But if we have multiple embedded objects in the class then this technique will not work. Because data base table could not have more than one column with the same name. Let’s say we have AddressOne, AddressTwo as two embedded objects in the UserDetails Class, and then when tries to embed both at the same time with same names hibernate it violates the database rules. We can solve this problem by overriding the default name with custom name. In hibernate this is possible with @AttributeOverride. This annotation helps us to reconfigure the Colum names. Let’s re-configure the street name first for object AddressOne. This can be done as follows


If we want to configure more than one attribute/field at a time we can also do this with @AttributeOverrides annotation



If we have two embedded objects we have to provide the overridden names for at least one object other will be considered default.

Now there could also be a possibility that we have a primary key that is a combination of more than one fields of another object in that case we have to use Embedded Object as our primark key. The annotation that hibernate provides us to implement this feature is called the @EmbeddedId. In simple words word we can say that if in any case we have to make an object as a primary key, hibernate provide us the way to do it by simply using @EmbeddedId annotation with the field.


In this tutorial we have learned various things.

  • Customized column names
  • Overridden Column names
  • Embedded objects

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!

About Author: Uzair Ahmed Khan is Technical Content Writer, Computer Software Blogger, and Software Engineer. He has a keen eye on Java Technologies and other Related Developments.

Leave a Reply

Your email address will not be published. Required fields are marked *