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.

1

2

3

4

5

6

7

8

@Column(name="STREET_NAME")

privateStringStreet;

@Column(name="CITY_NAME")

privateStringCity;

@Column(name="DISTRICT_NAME")

privateStringDistrict;

@Column(name="COUNTRY_NAME")

privateStringCountry;

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

1

2

@AttributeOverride(name="Street",

column=@column(name="HOME_STREET_NAME"))

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

1

2

3

4

5

6

7

8

9

10

@AttributeOverrides({

@AttributeOverride(name="Street",

column=@column(name="HOME_STREET_NAME")),

@AttributeOverride(name="City",

column=@column(name="HOME_CITY_NAME"))

@AttributeOverride(name="District",

column=@column(name="HOME_DISTRICT_NAME")),

@AttributeOverride(name="Country",

column=@column(name="HOME_COUNTRY_NAME"))

})

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.

1

2

@EmbeddedId

protectedEmployeePK empPK;

Conclusion:

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.