Я хочу сохранить DayOfWeek в моей базе данных PostgreSQL. Поэтому я использую конвертер, чтобы сохранить его как целое число. Но я открыт, чтобы хранить его как другие типы данных, если у вас есть лучшие предложения. Конвертация не происходит, так как я не вижу ошибок, которые я добавил. Сообщение об ошибке усиливает это. Нужно ли мне менять мой crudrepository или где моя ошибка? Моя сущность:
@Entity
@Table(name = "opening_hours")
public class OpeningHours {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
private Long id;
@Column(name = "day")
@Convert(converter = DayOfWeekIntegerConverter.class)
private DayOfWeek day;
Используемый мной CrudRepository выглядит следующим образом:
public interface OpeningHoursRepository extends CrudRepository<OpeningHours, Long> {
@Query(value = "SELECT closing_time FROM opening_hours WHERE day = :day_of_week", nativeQuery = true)
LocalTime getShopClosingTimeOn(@Param("day_of_week") DayOfWeek dayOfWeek);
Для хранения DayOfWeek я использую следующий конвертер:
@Converter // I tried (autoapply=true) already but did not change anything
public class DayOfWeekIntegerConverter implements AttributeConverter<DayOfWeek, Integer> {
@Override
public Integer convertToDatabaseColumn(DayOfWeek attribute) {
System.err.println("converting dayofweek to int");
return attribute.getValue();
}
@Override
public DayOfWeek convertToEntityAttribute(Integer dbData) {
System.err.println("converting int to dayofweek ");
return DayOfWeek.of(dbData);
}
}
MyТаблица базы данных выглядит следующим образом:
CREATE TABLE IF NOT EXISTS opening_hours
(
id BIGSERIAL NOT NULL PRIMARY KEY,
day INT
);
Вот ошибка:
org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause
...
org.postgresql.util.PSQLException: ERROR: operator does not exist: integer = bytea
Hinweis: No operator matches the given name and argument types. You might need to add explicit type casts.
Position: 51