这是我的模型类属性
@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。请给出解决方案以解决此问题
答案 0 :(得分:1)
也许
public String objectToSQLString(Date value, Dialect dialect)
应该是
public String objectToSQLString(Object value, Dialect dialect)
否则该方法不会被正确覆盖
答案 1 :(得分:0)
问题在于
createdate bigint(20) NULL
您应将此声明为datetime
尝试
alter table table1 modify createdate datetime
答案 2 :(得分:0)
您可以更改列
createdate as varchar(20)