当我的字段被JPA添加时,在特定时间它保存的日期和时间是正确的,其他的则不保存,只有一个日期是正确的,但是时间是零(00:00:00)。
13/11/2019 11:05:12
非Corretct:13/11/2019 00:00:00
我的数据库是postgresql,我的字段是timestamp类型的
在java类中,我的字段注释如下:
@Column(name = "DAT_ACTION")
private LocalDateTime espDatAction;我现在不知道发生了什么,因为应用程序的操作是相同的,并且有不同的行为
发布于 2019-11-13 15:36:37
如果要将LocalDate属性存储在DATE列中或LocalDateTime存储在时间戳列中,则需要自己定义到java.sql.Date或java.sql.Timestamp的映射。您可以尝试添加下面这样的内容并重新启动应用程序,它应该在它之后正确保存:
import java.time.LocalDateTime;
import java.sql.Timestamp;
@Converter(autoApply = true)
public class LocalDateTimeAttributeConverter implements AttributeConverter<LocalDateTime, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime locDateTime) {
return locDateTime == null ? null : Timestamp.valueOf(locDateTime);
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp sqlTimestamp) {
return sqlTimestamp == null ? null : sqlTimestamp.toLocalDateTime();
}
}发布于 2019-11-18 05:09:26
在JPA2.2新版本中使用这种方法
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern="dd/MM/yyyy HH:mm:ss")
private LocalDateTime espDatAction;https://stackoverflow.com/questions/58839896
复制相似问题