Wal*_*aby 5 java postgresql hibernate jpa spring-boot
我正在使用 JPA 2.3.1 和 JDBC 5.2.7,当我尝试使用 PostGreSQL DB 中的 JPA RepoLocalDateTime
作为参数类型来查询表时。它不断收到此错误:
Parameter value [2020-12-08T07:35] did not match expected type [java.time.LocalDateTime (n/a)]
Run Code Online (Sandbox Code Playgroud)
我还尝试添加@Convert
带有自定义类的自定义属性,该自定义类可以转换LocalDateTime
为Timestamp
但也不起作用。
EventEntity.class 具有如下属性:
@Column(name = "event_timestamp",nullable = false)
@JsonSerialize(using = CustomLocalDateTimeSerializer.class)
@Convert(converter = LocalDateTimePersistenceConverter.class)
LocalDateTime eventTimestamp;
Run Code Online (Sandbox Code Playgroud)
CustomLocalDateTimeSerializer
类只是序列化日期时间值以
"yyyy-MM-dd HH:mm:ss.SSS"
使用DateTimeFormatter
这种格式插入到列记录中。
LocalDateTimePersistenceConverter.class 转换如下:
@Converter(autoApply = true)
public class LocalDateTimePersistenceConverter implements AttributeConverter<LocalDateTime, java.sql.Timestamp> {
@Override
public java.sql.Timestamp convertToDatabaseColumn(LocalDateTime entityValue) {
if (entityValue != null) {
return java.sql.Timestamp.valueOf(entityValue);
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
用于搜索的类如下:
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class SearchCriteria {
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
LocalDateTime eventTimestamp;
}
Run Code Online (Sandbox Code Playgroud)
这是查询方法:
public void search(SearchCriteria sc){
Specification<EventEntity>
searchCriteria.add(new SearchCriteria("eventTimestamp",
SearchOperation.GreaterThanOrEqual , sc.getEventTimestamp()));
return new SpecificationsBuilder<EventEntity>(searchCriteria).build();
}
SearchCriteria sc = new SearchCriteria();
sc.setEventTimestamp(LocalDateTime.now());
Specification<EventEntity> searchSpecification =
search(sc);
List<EventEntity> entities = repository.findAll(searchSpecification);
Run Code Online (Sandbox Code Playgroud)
我根据研究实现了此转换,该版本的 JPA 尚无法自动转换为LocalDateTime
它正在使用的java.util.timestamp
.
归档时间: |
|
查看次数: |
2635 次 |
最近记录: |