Breaking Up A Large Hibernate Class

(Recovered from my old Blog).

Over time, Hibernate classes can get very large – especially when modelling a complicated object (for example a financial transaction).

At this point, the @Embedded annotation comes to the rescue. The class annotated with @Embedded will be linked into the parent class as if it were one large class.

This even works with collections – the collection can be defined in the child (@Embeddable) class.

One caveat – we ought to be programming to interfaces rather than implementations. Assuming we marked an interface as @Embedded, Hibernate will scan the interface and not import any of the fields defined in the class. In this case, one simply adds the @Target annotation:

If using Criteria, this extra ‘level’ in the class hierarchy is simply referenced via a full-stop:

Leave a Reply

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