Recreating Seam’s EntityHome in CDI

I mention constantly how working with Seam made life easier. One of the aspects of Seam I grew to love was EntityHome which provided a consistent way of interacting with an Entity. Methods and functionality which is common to most CRUD (Create, Read, Update, Delete) styled pages.

In a recent project a lot of the screens essentially provided CRUD so I created a BaseController which provided much of the same functionality as the old EntityHome and could be extended and used to quickly create new pages for new entities.

A typical usage would be

This would give us basic operations of save(), update(), delete(), create(), find().

You probably have noticed a little magic in the postconstruct method. Here we extract the entity class defined in the generics at runtime so that we know which type of class to use to create new instances.  For the sake of completeness I’ll include the contents of GenericsUtil as well at the end of this post.

Apart from that we also have methods to list() all entities of the given type and we can override listQuery() in order to customise the SQL used to retrieve that list.

It would be easy to extend this further to inject the current Conversation or other common elements.

GenericsUtil

In the above example we recursively iterate up the inheritance hierarchy looking for a ParameterizedType whose Parameters Type is annotated with @Entity (marking that class as a JPA entity) This is the class type that we can then use in our EntityHome.