Skip to Main Content
Hibernate: A Developer's Notebook
book

Hibernate: A Developer's Notebook

by James Elliott
May 2004
Intermediate to advanced content levelIntermediate to advanced
192 pages
4h 46m
English
O'Reilly Media, Inc.
Content preview from Hibernate: A Developer's Notebook

7.2. Using a Custom Type Mapping

All right, we've created a custom type persistence handler, and it wasn't so bad! Now it's time to actually use it to persist our enumeration data the way we want it.

7.1.1. How do I do that?

This is actually almost embarrassingly easy. Once we've got the value class, SourceMedia, and the persistence manager, SourceMediaType, in place, all we need to do is modify any mapping documents that were previously referring to the raw value type to refer instead to the custom persistence manager.

In our case, that means we change the mapping for the mediaSource property in Track.hbm.xml so it looks like Example 7-2 rather than Example 6-3.

That's it. No, really!.

Example 7-2. Custom type mapping for the sourceMedia property
<property name="sourceMedia" type="com.oreilly.hh.SourceMediaType">
  <meta attribute="field-description">Media on which track was obtained</meta>
  <meta attribute="use-in-tostring">true</meta>
</property>

At this point, running ant schema will rebuild the database schema, changing the SOURCEMEDIA column in the TRACK table from integer to VARCHAR(as specified by SourceMediaType's sqlTypes() method).

Thanks to the beauty of letting the object/relational mapping layer handle the details of how data is stored and retrieved, we don't need to change any aspect of the example or test code that we were using in Chapter 6. You can run ant ctest to create sample data. It will run with no complaint. If you fire up ant db to look at the way it's stored, ...

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

Getting Started with Hibernate 3

Getting Started with Hibernate 3

James Elliott

Publisher Resources

ISBN: 0596006969Supplemental ContentCatalog PageErrata