![](/img/trans.png)
[英]JPA: how do I persist a String into a database field, type MYSQL Text
[英]How do I persist a MySQL timestamp field as a long (epoch) field in java using JPA?
我在MySQL表中有一列,例如:
createdAt timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP`.
我希望將其存儲在如下字段中:
private Long createdAt;
我該怎么做?
如果您在TemporalType
枚舉中使用受支持的類型,則最好。 多頭需要轉換器。
這是一個長期的轉換器:
import java.sql.Timestamp;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply = true)
public class LongConverter implements AttributeConverter<Long, Timestamp> {
@Override
public Timestamp convertToDatabaseColumn(Long attribute) {
if (attribute == null)
return null;
return new Timestamp(attribute);
}
@Override
public Long convertToEntityAttribute(Timestamp dbData) {
if (dbData == null)
return null;
return dbData.getTime();
}
}
簡單地使用@Temporal(TemporalType.TIMESTAMP)
以上createdAt
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.