JPA Attribute Converter: Difference between revisions
Jump to navigation
Jump to search
(9 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
* https://www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter | * https://www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter | ||
* https://www.thoughts-on-java.org/jpa-21-type-converter-better-way-to/ | * https://www.thoughts-on-java.org/jpa-21-type-converter-better-way-to/ | ||
* https://www.thoughts-on-java.org/how-to-use-jpa-type-converter-to/ | |||
=Internal= | =Internal= | ||
Line 10: | Line 11: | ||
=Overview= | =Overview= | ||
An attribute converter allows the developer to specify methods to convert between the database and the Java representation of an attribute. | An attribute converter allows the developer to specify methods to convert between the database and the Java representation of an attribute. Attribute conversion was introduced in JPA 2.1. All entity class. mapped superclass or embeddable class attributes can be type-converted, with the following exceptions: | ||
* identity ([[@Id]]) attributes | |||
* version ([[@Version]]) attributes | |||
* relationship attributes ([[@OneToOne]], [[@OneToMany]], [[@ManyToOne]], [[@ManyToMany]]) | |||
* attributes annotated as [[@Temporal]] or [[@Enumerated]]. | |||
=Playground Example= | =Playground Example= | ||
Line 20: | Line 25: | ||
==@Convert Annotation== | ==@Convert Annotation== | ||
Annotate the entity fields to be converter with @javax.persistence.Convert: | Annotate the entity fields to be converter with [[@Convert|@javax.persistence.Convert]]: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
Line 29: | Line 34: | ||
==The Converter Class== | ==The Converter Class== | ||
Provide a converter class, which must implement AttributeConverter<''Entity-Field-Type'', ''Database-Type''> and be annotated with @javax.persistence.Converter: | {{External|https://docs.oracle.com/javaee/7/api/javax/persistence/AttributeConverter.html}} | ||
Provide a converter class, which must implement AttributeConverter<''Entity-Field-Type'', ''Database-Type''> and be annotated with [[@Converter|@javax.persistence.Converter]]: | |||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> |
Latest revision as of 06:46, 1 November 2018
External
- https://www.thoughts-on-java.org/jpa-21-how-to-implement-type-converter
- https://www.thoughts-on-java.org/jpa-21-type-converter-better-way-to/
- https://www.thoughts-on-java.org/how-to-use-jpa-type-converter-to/
Internal
Overview
An attribute converter allows the developer to specify methods to convert between the database and the Java representation of an attribute. Attribute conversion was introduced in JPA 2.1. All entity class. mapped superclass or embeddable class attributes can be type-converted, with the following exceptions:
- identity (@Id) attributes
- version (@Version) attributes
- relationship attributes (@OneToOne, @OneToMany, @ManyToOne, @ManyToMany)
- attributes annotated as @Temporal or @Enumerated.
Playground Example
Programming Model
@Convert Annotation
Annotate the entity fields to be converter with @javax.persistence.Convert:
@Convert(converter = PayloadConverter.class)
private Payload payload;
The Converter Class
Provide a converter class, which must implement AttributeConverter<Entity-Field-Type, Database-Type> and be annotated with @javax.persistence.Converter:
@Converter
public class PayloadConverter implements AttributeConverter<Payload, String> {
@Override
public String convertToDatabaseColumn(Payload attribute) {
...
}
@Override
public Payload convertToEntityAttribute(String dbData) {
...
}
}