Skip to Content
Harnessing Hibernate
book

Harnessing Hibernate

by James Elliott, Timothy M. O'Brien, Ryan Fowler
April 2008
Intermediate to advanced content levelIntermediate to advanced
380 pages
11h 26m
English
O'Reilly Media, Inc.
Content preview from Harnessing Hibernate

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Beginning Hibernate: For Hibernate 5

Beginning Hibernate: For Hibernate 5

Joseph B. Ottinger, Jeff Linwood, Dave Minter
Just Hibernate

Just Hibernate

Madhusudhan Konda
Beginning Hibernate, Third Edition

Beginning Hibernate, Third Edition

Joseph B. Ottinger, Dave Minter, Jeff Linwood

Publisher Resources

ISBN: 9780596517724Supplemental ContentErrata Page