liveBook · Manning

Type definition

As explained in Java Persistence with Spring Data and Hibernate epub

A type definition is reusable Hibernate mapping metadata that binds a custom type adapter to a name and its parameters.

A type definition names reusable Hibernate mapping metadata that binds a custom type adapter to parameters. Declare definitions once, commonly in package-info.java, then reference them by name in property mappings. section 6.3.3

Currency-specific monetary mappings

Define monetary_amount_usd and monetary_amount_eur with the same MonetaryAmountUserType but different convertTo parameters. Map Item properties with @Type(type = "monetary_amount_usd") or @Type(type = "monetary_amount_eur"). Because the adapter uses two columns, declare both with @Columns and preserve their order. section 6.3.3

Path: Ch06/mapping-value-types4/src/main/java/com/manning/javapersistence
 /ch06/converter/package-info.java
 
@org.hibernate.annotations.TypeDefs({
    @org.hibernate.annotations.TypeDef(
        name = "monetary_amount_usd",
        typeClass = MonetaryAmountUserType.class,
        parameters = {@Parameter(name = "convertTo", value = "USD")}
    ),
    @org.hibernate.annotations.TypeDef(
        name = "monetary_amount_eur",
        typeClass = MonetaryAmountUserType.class,
        parameters = {@Parameter(name = "convertTo", value = "EUR")}
    )
})
package com.manning.javapersistence.ch06.converter;
import org.hibernate.annotations.Parameter;

Shows how one adapter becomes reusable named definitions for different conversion targets. section 6.3.3

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains declaration, parameter reuse, property selection, and multi-column ordering, but defers the adapter implementation details.
Examples
Uses USD and EUR MonetaryAmount definitions mapped to Item prices across amount and currency columns.
What it assumes
Assumes familiarity with Hibernate custom type adapters, annotations, and entity property mappings.
What you can do after
Apply named Hibernate type definitions to avoid repeating adapter parameters in property mappings.
How it connects
Connects UserType, CompositeUserType, DynamicParameterizedType, @Type, and @Columns.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.