Hibernate / EclipseLink Interview questions
What is the @Convert annotation used for?
@Convert, paired with a class implementing AttributeConverter, lets an entity attribute be stored in the database as a different type than its Java representation, converting between them automatically on read and write. A common example is storing a Java enum or a custom value object as a plain string or integer column.
@Converter(autoApply = true) public class StatusConverter implements AttributeConverter<Status, String> { @Override public String convertToDatabaseColumn(Status status) { return status == null ? null : status.getCode(); } @Override public Status convertToEntityAttribute(String code) { return Status.fromCode(code); } }
Standard JPA's AttributeConverter covers most cases, but EclipseLink also ships its own broader set of native converters (like Serialized or Struct converters) for scenarios the standard converter interface doesn't directly address, such as mapping to database-specific structured types.
More Related questions...