liveBook · Manning

@Convert annotation

As explained in Java Persistence with Spring Data and Hibernate epub

The @Convert annotation declares a JPA converter for a persistent attribute or a nested attribute path.

The @Convert annotation assigns a JPA AttributeConverter to a persistent attribute, allowing application-specific values to be stored in a different database representation and reconstructed when loaded. section 6.3.2

Converting a monetary value

@Convert(converter = MonetaryAmountConverter.class) @Column(name = "PRICE", length = 63) private MonetaryAmount buyNowPrice; The converter stores MonetaryAmount as text such as "11.23 USD" and recreates the value from the database column. section 6.3.2

Use the annotation to:

  • Map properties of entity classes or embeddable classes. section 6.3.2
  • Target a nested attribute path such as "city.zipcode" within an embedded address. section 6.3.2
  • Replace built-in Hibernate type adapters for selected domain properties. section 6.3.2

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Explains placement and bidirectional conversion, but leaves converter implementation and schema redesign to related mappings.
Examples
Uses MonetaryAmount-to-string conversion and a nested city.zipcode conversion inside an embedded address.
What it assumes
Assumes entity and embeddable mappings while relying on AttributeConverter for custom conversion behavior.
What you can do after
Apply it via JPA to persist custom value types without immediately changing the database schema.
How it connects
Connects AttributeConverter with entity attributes, embeddable classes, Hibernate type adapters, and @Embeddable mappings.

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.