Zum Beispiel:
Code: Select all
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);
}
}
Code: Select all
@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);
}
}
Code: Select all
@Entity
public class MyEntity {
@Id
private Long id;
@Convert(converter = CommitteeTypeConverter.class)
private CommitteeType committeeType;
}
Wenn ich von DTO zu Entity zuordne, wird die Aufzählung korrekt zugewiesen, aber JPA speichert den Namen der Aufzählungskonstante (
Code: Select all
AUDIT_COMMITTEECode: Select all
"audit committee"Der Konverter scheint nicht ausgelöst zu werden. Was übersehe ich?
Mobile version