【发布时间】:2020-02-28 00:03:01
【问题描述】:
我想在我的 PostgreSQL 数据库中存储 DayOfWeek。因此我使用转换器将其存储为整数。但如果您有更好的建议,我愿意将其存储为其他数据类型。 转换不会发生,因为我没有看到我添加的错误打印。错误消息加强了这一点。 我是否必须更改我的 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);
}
}
我的数据库表如下所示:
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
【问题讨论】:
-
您能否向我们提供一条错误消息或其他向我们展示您遇到的问题的信息?
-
添加了错误信息。将其保存为日期是没有选择的,因为我只对一周中的哪一天感兴趣。不想处理任何特定日期
-
修改了我的答案@juuubbb
-
你在修改后的答案中尝试过我的建议吗
标签: java spring postgresql jpa