6 Mapping value types
This chapter covers
- Mapping basic properties
- Mapping embeddable components
- Controlling mapping between Java and SQL types
After spending the previous chapter almost exclusively on entities and the respective class- and identity-mapping options, we now focus on value types in their various forms. Values types are frequently encountered in the classes under development. We split value types into two categories: basic value-typed classes that come with the JDK, such as
Date, primitives, and their wrappers; and developer-defined value-typed classes, such as
MonetaryAmount in CaveatEmptor.
In this chapter, we first map persistent properties with JDK types and learn the basic mapping annotations. We’ll see how to work with various aspects of properties: overriding defaults, customizing access, and generated values. We’ll also see how SQL is used with derived properties and transformed column values. We’ll work with basic properties, temporal properties, and mapping enumerations. We then examine custom value-typed classes and map them as embeddable components. We’ll learn how classes relate to the database schema and make the classes embeddable while allowing for overriding embedded attributes. We complete embeddable components by mapping nested components. Finally, we analyze how to customize loading and storing of property values at a lower level with flexible JPA converters, a standardized extension point of every JPA provider.