0

这是我的模型类属性

@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。请给出解决此问题的解决方案

4

3 回答 3

1

也许

public String objectToSQLString(Date value, Dialect dialect)

应该

public String objectToSQLString(Object value, Dialect dialect)

否则该方法没有被正确覆盖

于 2012-12-21T15:12:47.500 回答
0

你可以改变你的专栏

createdate as varchar(20)
于 2012-12-21T14:33:43.913 回答
0

问题在于

createdate  bigint(20) NULL

您应该将其声明为datetime

尝试

alter table table1 modify createdate datetime 
于 2012-12-21T12:53:47.010 回答