Search code examples
javahibernatevalidationstrategy-pattern

Implementing the Strategy Pattern against generated pojos


I am writing a parser for a couple of different DB tables. We're using Hibernate with Eclipse, and we've made hbm.xml mapping files that correspond to our tables so that our .java files are generated in line with the tables.

We've added a new table called Gamer containing the usual user stuff (address name phone# etc). It's not related to the established Customer table(also containing address name phone# etc), but there's a lot of shared behaviour in the validation steps.

I think this would be ripe for applying the Strategy design pattern to, the problem being that the Customer POJO and the Gamer POJO aren't inheriting from anything, and they are being defined off of independent unrelated tables.

I'm quite new to design patterns and I'm rather wary that I may be being an utter dumbass so any suggestions on how I might go forward and share the validation logic, without having to resort to having CustomerAddressValidator and GamerAddressValidator classes which do the exact same thing.


Solution

  • First of you have to distinguish whether the Gamer is a Customer, or Gamer and Customer are both Persons.

    In both cases you will have some base class and extended class(es). In Hibernate (and JPA) there are three ways to handle inheritance: Single Table, Joined and Table Per Class. All three methodologies have prons and cons, so you have to chose one based on your concrete domain problem. More about ineritance you can read here(JPA) and here(Hibernate).

    After that, you will be able to write single Validator for base class (if all required values for validation are placed in base class) and call that validator for both base and extended class.

    As I can from your answer you need Address validator, sou you have to put address in the base class. Hope this help.