onsdag, december 14, 2005

Unorthodox Hibernate.

Today I did something slightly unorthodox with Hibernate. Or at least I believe it is. And I couldn't find a better way of doing it. You be the judge.

First, some background. Most of the system I'm working on is fairly normal with JavaBeans and whatnot. But we do have twenty-odd of something called types. These types are supposed to be our own implementation of the type-safe enum pattern, with the added complexity that the values of these enums is saved in the database, each value have one or many localized strings attached to it. So for, so good. The old solution was based on some sql and some reflective calling of static getInstance-methods. This was clearly not possible with Hibernate. Now, let me be clear about this. What was really the point was getting Hibernate to return the same instance for the same key every time.

After some fiddling about with this, it turned out that the best solution was to go grope around the innards of Hibernate and implement my own EntityPersister for this task. (By the way, if you should go down this route - and are using Hibernate 3 - be aware that the API-docs are not totally up to date, regarding demands on constructor signature and such.)

According to all documentation I could find, implementing your own EntityPersister was not something that should ever be needed by a user of Hibernate. Clearly this is not so, or I'm missing something obvious. So, my question is simple, how should I have managed this with existing Hibernate tools? And no, modifying the model objects are not an option. To much dependencies.

Inga kommentarer: