liveBook · Manning

AttributeConverter

As explained in Java Persistence with Spring Data and Hibernate epub

AttributeConverter is the JPA interface for defining conversions between an entity attribute type and a database column type.

AttributeConverter defines a bidirectional mapping between an entity attribute type and a database column type. A monetary value can therefore persist as a single VARCHAR such as “11.23 USD” and reappear as MonetaryAmount. section 6.3.2

MonetaryAmount stored as text

Annotate the converter with @Converter and implement AttributeConverter<MonetaryAmount, String>. Convert the domain value in convertToDatabaseColumn and reconstruct it in convertToEntityAttribute; apply it with @Convert(converter = MonetaryAmountConverter.class) on Item#buyNowPrice. section 6.3.2

Apply the same extension point in several mapping situations:

  • Convert properties of entities or embeddable classes. section 6.3.2
  • Target nested component properties with attributeName, such as city.zipcode. section 6.3.2
  • Override Hibernate’s built-in adapters, including selected java.util.Date properties. section 6.3.2
Listing 6.16 Converting between strings and MonetaryValue
Path: Ch06/mapping-value-types2/src/main/java/com/manning/javapersistence
 /ch06/converter/MonetaryAmountConverter.java
 
@Converter                                                                 #1
public class MonetaryAmountConverter                                       #1
  implements AttributeConverter<MonetaryAmount, String> {                  #1
 
  @Override                                                                #2
  public String convertToDatabaseColumn(MonetaryAmount monetaryAmount) {   #2
      return monetaryAmount.toString();                                    #2
  }                                                                        #2
 
  @Override                                                                #3
  public MonetaryAmount convertToEntityAttribute(String s) {               #3
      return MonetaryAmount.fromString(s);                                 #3
  }                                                                        #3
}
#1 A converter has to implement the <kbd>AttributeConverter</kbd> interface; the two arguments are the type of the Java property and the type in the database schema. The Java type is <kbd>MonetaryAmount</kbd>, and the database type is <kbd>String</kbd>, which maps, as usual, to an SQL <kbd>VARCHAR</kbd>. We must annotate the class with <kbd>@Converter</kbd>.
#2 The <kbd>convertToDatabaseColumn</kbd> method will convert from the <kbd>MonetaryAmount</kbd> entity type to the string database column.
#3 The <kbd>convertToEntityAttribute</kbd> method will convert from the string database column to the <kbd>MonetaryAmount</kbd> entity type.

Inspect the converter implementation that connects MonetaryAmount with its string database representation. section 6.3.2

Converters can be removed when the schema evolves: replacing one encoded column with separate amount and currency columns allows MonetaryAmount to become an @Embeddable, while upgraded and legacy tables can coexist selectively. section 6.3.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Defines both conversion methods, annotation placement, nested-property targeting, and migration implications.
Examples
Uses MonetaryAmount-to-VARCHAR conversion and Zipcode conversion within a nested address component.
What it assumes
Assumes entity and value-type mapping knowledge while demonstrating conversion through concrete domain classes.
What you can do after
Build a bidirectional JPA converter and apply it to entity or embeddable properties.
How it connects
Connects entity attributes, embeddable classes, value types, Hibernate type adapters, and database schema evolution.

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.