Chapter 6. Custom Value Types
Defining a User Type
Hibernate supports a wealth of Java types—both simple values and objects—as you can see by skimming Appendix A. By setting up mapping specifications, you can persist even highly complex, nested object structures to arbitrary database tables and columns. With all this power and flexibility, you might wonder why you’d ever need to go beyond the built-in type support.
One situation that might motivate you to customize Hibernate’s
type support is if you want to use a different
SQL column type to store a particular Java type than
Hibernate normally chooses. The reference documentation cites the example
of persisting Java BigInteger
values into VARCHAR
columns, which might be necessary
to accommodate a legacy database schema.
Another scenario that is very common involves persisting
enumerated type values. Prior to Java 5, there was no built-in
language support for enumerations, so although Joshua Bloch’s excellent
pattern presented in Effective Java Programming Language
Guide (Addison-Wesley) was a de facto standard, Hibernate had
to be agnostic about how to support the concept. The
PersistentEnum
interface that it provided prior to
Hibernate 3 wasn’t suited to the native enum
support introduced in Java 5, so it has
gone away. Unfortunately, nothing has arisen to replace it within
Hibernate itself, so we’ll show you how to leverage the user type support
for this purpose.
Another scenario that requires the ability to tweak the type system ...
Get Harnessing Hibernate now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.