这是我的模型类属性
@Column(name = "createdate")
@Type(type="long_timestamp")
private Date creationDate;
这是我的类型定义
@TypeDef(
name="long_timestamp",
typeClass = com.x.LongTimestampType.class
),
请参考我的 LongTimestampType.java
public class LongTimestampType extends TimestampType {
@Override
public String objectToSQLString(Date value, Dialect dialect)
throws Exception {
return "" + ((Date) value).getTime();
}
@Override
public Object get(ResultSet rs, String index) throws HibernateException, SQLException {
return new Date(rs.getLong(index));
}
@Override
public void set(PreparedStatement ps, Date value, int index, SessionImplementor sessionImplementor) throws HibernateException, SQLException {
System.out.println("set Date as BIGINT into Prepared Statement :: " + ((Date)value).getTime());
ps.setLong(index, ((Date)value).getTime());
}
@Override
public String getName() {
return "long_timestamp";
}
public int[] sqlTypes() {
return new int[] { Types.BIGINT };
}
/* public int sqlTypes() {
return Types.BIGINT;
}*/
}
这是我的 mySql 表列
createdate bigint(20) NULL
我保存这个对象的java代码是
entityManager.persist(object);
我的问题是,当我尝试将对象保存到表中时,它会抛出:java.sql.BatchUpdateException: Data truncated for column 'createdate' at row 1
即使我也将null设置为 createDate。请给出解决此问题的解决方案