State Pattern Persistence with Hibernate

In my software development job, I do a lot of persistence layer implementation, especially using the Hibernate O/R mapper. I work closely with a clever domain layer programmer who tends to use a lot of design patterns, perhaps because he likes the woman of the cover of Head First Design Patterns.

Sometimes, these design patterns create a bit of a challenge for me in the persistence layer, especially since I always tell my domain layer programmer that I can persist things transparently, without having to muck up his code with persistence layer artifacts.

A case in point is the State pattern. I have now persisted 5 different sets of states so it’s no longer a challenge. However, it took me a little while to figure how to do it. Naturally, I started by checking out Hibernate’s forums but much to my chagrin, I found several people asking the same question but no one offering an answer. Therefore, I will provide an answer in this blog post. Continue reading » State Pattern Persistence with Hibernate