The Generation Gap Pattern, or GG from now on, is quite simple - you have your ORM objects like Business or Client or what have you generated as "_Business", or as "Business" but in a seperate namespace. Then, your actual class is Business extends _Business. This makes it easy to put object specific helper methods and properties into the Business class and not have it get wiped every time you re-generate the classes (using hbm2java in this case). Fine, it's very useful.
Now, Hibernate lets you do this, but it's not obvious how, and the very little mentioned about it is forum posts that are almost 5 years old. They still work, but they're quite sparse. There's also a couple of nasty gotchas. So, here's a base example (from my current game Capo):
<class name="Capo" table="capo" catalog="gangster">
<meta attribute="generated-class" inherit="false">com.tamedtornado.data.base._Capo</meta>
<meta attribute="extends" inherit="false">com.tamedtornado.data.Entity</meta>
<id name="id" type="java.lang.Integer">
<column name="capo_id">
<generator class="identity">
</id>
Ok, now the XML formatting is awful, I know, but you should see what you need to do. I've bolded the important line.. That gets you a base class named _Capo in .base, and one important bit to note is the inherit="false" attribute. Very important - if you don't include that, weird things will happen. The first set in your file will be written out as your base class when you run the code generator. Ugh. Took me a while to figure that one out.
The line under that is also useful - you can specify a base class for this Hibernate object to inherit from, so _Capo extends Entity in this case. In my case, Entity holds the HashCode, Equals, ID, etc code to make things work consistently.
I hope that helps someone.
0 comments:
Post a Comment