In the blog Prashant Gupta demonstrate how to use embedded property in hibernate. We can do the same thing in grails using embedded property of grails domain class.

Syntax: static embedded = [‘address’, ‘payment’]

Purpose: Supports embedding domain components into domain classes.

Description: An embedded component does not store its data in its own table as a regular domain class relationship does. Instead, the data is included in the owner’s table. This means that queries are faster because there is no join required, but you may end up with duplicate data across tables.

Use case where it makes sense: An Employee domain which will contain properties like account number, account holder name, bank name, branch name, IFSC code. And (important) they won’t be referred to by any other domain class in the domain model then it makes sense to group these properties and embed it as BankAccount in the owning domain class. Now BankAccount will have its existence through parent domain only.

Use case where it does not make sense: A Delivery domain which contains the info of auctioned item and needs the Address where the item will be dispatched. Here it makes sense to create a separate persistent entity for Person’s Address with its own independent life cycle and object id so that it can be referred from the Delivery domain.

Lets see by example:

Normal(composition) way:



Here Grails create two tables, one for User and another for Address and User contains id of the Address domain.


And whenever we want address of a User then we can get it by user.address for which hibernate fire another query.

Now embedded way:



Note: The Address class should be in src/groovy, if you put this in domain package then grails create a table for this.

Here Grails create a single table of User which have address fields as well. And hence whenever we fetch User address is also available (No extra DB query).


No Join. No extra DB hit.
If developers are planing to put all the data(eg. name, email, number, code) in one table then this approach provide this facility plus much more readability and clarity.

We can not reuse address (duplicate data across tables).
Pull all the data.
Table get heavier.

Ref: grails domain’s embedded property

Hope this helps 🙂 . If you have something to add here, please do share, Thanks.