Message
I have run into a
couple of problems using byte [] types as primary keys. Not realizing the
specification does not explicitly support this type mapping, we pursued this
mapping strategy against a legacy data model which had RAW(16)
pk's. Nontheless, weve run into problems when Entities with byte []
typed primary keys are used in relationships e.g.: Infinite loops,
ClassCast Exceptions, and invalid Cache misses. IMHO, glassfish should support
this use case and the JPA should explicitly support this use case in an a future
specification revision.
Folks often use fixed length RAW column types as primary keys when they need to
store UUIDs, I personally, have run into this at least twice in the last your or
so, and some of my collegues confirmed that this has become more, rather
than less, common: the introduction of the java.util.UUID in Java 5, for
example. Further, it seems more than reasonable, for an ORM solution to support
fixed byte array mappings to a fixed RAW column --it is a quite natural mapping
in my mind. While it is feasible for some folks to change data
models e.g. storing the hex values in character based columns, this does not
work for legacy data models due to significant data migration efforts, which in
turn could hamper JPA adoption for these folks.
After looking at the
glassfish source code, it was clear that at some point this was supported, or at
least intentded to be supported ,so I decided to resolve the issue and
provide a fix for the broader community with the hope that it will integrated
into the project. I have been testing this fix for a couple of weeks and
have yet to encounter any issues. I am attaching the source for my byte [] fix.
I did refactor a couple of things to enhance readability, and fixed a
similar bug with char [] which should be supported too IMHO. If anyone
else runs into any issues using this fix with byte/char [] pk's please
drop me a note and would happy to try and provide a fix.
Thanks,
Adam