Например:
Код: Выделить всё
public enum CommitteeType {
AUDIT_COMMITTEE("audit committee"),
EXECUTIVE_COMMITTEE("executive committee");
private final String description;
CommitteeType(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public static CommitteeType fromDescription(String desc) {
for (CommitteeType type : values()) {
if (type.description.equals(desc)) {
return type;
}
}
throw new IllegalArgumentException("Invalid description: " + desc);
}
}
Код: Выделить всё
@Converter
public class CommitteeTypeConverter implements AttributeConverter {
@Override
public String convertToDatabaseColumn(CommitteeType committeeType) {
if (committeeType == null) {
return null;
}
return committeeType.getDescription();
}
@Override
public CommitteeType convertToEntityAttribute(String s) {
if (s == null) {
return null;
}
return CommitteeType.fromDescription(s);
}
}
Код: Выделить всё
@Entity
public class MyEntity {
@Id
private Long id;
@Convert(converter = CommitteeTypeConverter.class)
private CommitteeType committeeType;
}
Когда я сопоставляю DTO с Entity, перечисление назначается правильно, но JPA сохраняет имя константы перечисления (
Код: Выделить всё
AUDIT_COMMITTEEКод: Выделить всё
"audit committee"Похоже, конвертер не запускается. Что мне не хватает?
Подробнее здесь: https://stackoverflow.com/questions/798 ... ion-string
Мобильная версия