Hibernate версии 6+ и Spring Security версии 6+. База данных - попрес.
Код: Выделить всё
@Getter
@Setter
@Entity
@Table(name="test_model")
public class TestModel {
@Id
@GeneratedValue(generator = "UUID")
@UuidGenerator
@Column(name = "id", updatable = false, nullable = false)
private UUID id;
@CreationTimestamp
@Column(name="created")
private Date created;
@UpdateTimestamp
@Column(name="last_modified")
private Date lastModified;
@Column(name="quantity")
private Integer quantity;
@Enumerated(EnumType.STRING)
@Column(name="create_process_status")
private ProcessStatusType processStatusType;
}
< /code>
Мой класс Enum похож на ниже < /p>
public enum ProcessStatusType {
completed("completed"), processing("processing"), failed("failed");
private String type;
private ProcessStatusType(String type) {
this.type = type;
}
public String getType() {
return type;
}
}
< /code>
В базе данных также я создал столбец типа enum < /p>
create_process_status schema.enum_process_status_type DEFAULT 'completed'::schema.enum_process_status_type,
< /code>
Из класса службы мой код такой: < /p>
public String save() {
TestModel testModel = new TestModel();
testModel.setQuantity(2);
testModel.setCreated(XXXX);
testModel.setlastModified(YYYYY);
testModel.setProcessStatusType(ProcessStatusType.processing);
MyRepository.save(testModel);
return " created successfully " ;
}
< /code>
Но при запуске API я получаю ошибку, подобную ниже: < /p>
Ошибка: столбец "create_process_status" имеет типа Schema.enum_process_status_type, но выражение имеет значение типа
. Созданный класс пользовательского преобразователя также для обработки enum. < /p>
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class ProcessStatusTypeConverter implements AttributeConverter
{
@Override
public String convertToDatabaseColumn(ProcessStatusType attribute) {
if (attribute == null) {
return null;
}
return attribute.getType(); // Returns 'completed', 'processing', 'failed'
}
@Override
public ProcessStatusType convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
return ProcessStatusType.valueOf(dbData.toUpperCase()); // Converts back to the enum
}
}
< /code>
В модели я сделал изменения, как ниже < /p>
@Convert(converter = ProcessStatusTypeConverter.class) // Use the converter
@Column(name = "create_process_status")
private ProcessStatusType processStatusType;
< /code>
В pom.xml я добавил это < /p>
org.postgresql
postgresql
42.6.0
Пожалуйста, помогите мне с некоторыми предложениями.
Подробнее здесь: https://stackoverflow.com/questions/795 ... -enum-prop