Friday, February 11, 2011

JPA Entity: extending vs embedding

http://en.wikibooks.org/wiki/Java_Persistence/Embeddables#Example_of_an_Embeddable_object_annotations



JPA defines 2 annotations: @Embeddable and @Embedded
This is a great way to COMPOSE objects - rather than inheriting.

Suppose you have to incorporate in your entities several features:
tracking, logical deletion, id....

one solution is to stick all the co,,on features in a base abstract Entity.
The problem arises when not ALL Entities use ALL the features...; then you should introduce several base classes... this could soon become unmanageable.

The alternative is to Embed only the Embeddable entities that you want to incorporate.

The problem is that it's not easy to determine which Embeddable Entities are embedded in a given Entity.... while when using abstract classes or interfaces you can use instanceof.

No comments: