liveBook · Manning

Converter

As explained in Java Persistence with Spring Data and Hibernate epub

A custom type converter is application-defined conversion logic that translates values between a domain type and a persistence representation.

A converter supplies application-defined logic for translating a domain value to a persistence representation and back. JPA converters provide a standardized extension point for customizing how property values are stored and loaded. section 6.0 section 6.3.2

Implement javax.persistence.AttributeConverter<X, Y>, where X is the domain type and Y is the database-facing Java type. Mark the implementation with @Converter. The convertToDatabaseColumn method translates X to Y for storage; convertToEntityAttribute reconstructs X from Y when loading. section 6.3.2

Store a monetary amount as a string

A MonetaryAmount combines a value and a currency. A converter can map it to a String such as “11.23 USD” for a VARCHAR column and reconstruct the amount when it is loaded. This lets the application introduce multiple currencies before the database has separate amount and currency columns. section 6.3.2

Path: Ch06/mapping-value-types/src/main/java/com/manning/javapersistence
 /ch06/model/Item.java
 
@NotNull
@Convert(converter = MonetaryAmountConverter.class)
@Column(name = "PRICE", length = 63)
private MonetaryAmount buyNowPrice;

Apply the converter to the buyNowPrice property with @Convert and map the resulting value to the PRICE column. section 6.3.2

When the schema gains separate amount and currency columns, MonetaryAmount can instead become an @Embeddable mapped to those columns. Converters can be selectively enabled or disabled during a partial schema migration. section 6.3.2

Convert a nested value

A Zipcode converter can store a zipcode as a string and choose a concrete subtype when loading it. An @Convert annotation on an embedded address can target the nested property with attributeName = "city.zipcode". section 6.3.2

Converters can also adapt legacy temporal values, for example by converting a java.sql temporal value to a plain java.util.Date or giving a loaded Calendar a non-default time zone. Hibernate’s native UserType API is another, lower-level way to write an adapter. section 6.3.1 section 6.4

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains bidirectional AttributeConverter methods, property and nested mappings, and an alternative for a later schema migration.
Examples
Uses monetary amounts stored as currency-bearing strings and zipcodes reconstructed as concrete subtypes.
What you can do after
Apply conversion via JPA AttributeConverter and @Convert to basic or nested properties.
How it connects
Connects converters to embeddable components, basic property mappings, and Hibernate UserType adapters.

Also explained in

Compare how 4 books explain converter →

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.