JPA Attribute Converter: Difference between revisions
Jump to navigation
Jump to search
Line 33: | Line 33: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
@Converter | @Converter | ||
public class PayloadConverter implements AttributeConverter< | public class PayloadConverter implements AttributeConverter<Payload, String> { | ||
@Override | @Override | ||
public String convertToDatabaseColumn( | public String convertToDatabaseColumn(Payload attribute) { | ||
... | |||
} | } | ||
@Override | @Override | ||
public | public Payload convertToEntityAttribute(String dbData) { | ||
... | |||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 20:24, 31 October 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/
Internal
Overview
An attribute converter allows the developer to specify methods to convert between the database and the Java representation of an attribute.
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) {
...
}
}