Reputation: 749
This is my model class attribute
@Column(name = "createdate")
@Type(type="long_timestamp")
private Date creationDate;
This is my Type definition
@TypeDef(
name="long_timestamp",
typeClass = com.x.LongTimestampType.class
),
Please refer my 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;
}*/
}
This is my mySql table column
createdate bigint(20) NULL
My java code to save this object is
entityManager.persist(object);
My Problem is, When i trying to save Object into table it'll throw: java.sql.BatchUpdateException: Data truncated for column 'createdate' at row 1
Even i set null to createDate also. Please give solution to resolve this problem
Upvotes: 0
Views: 6300
Reputation: 2071
Perhaps
public String objectToSQLString(Date value, Dialect dialect)
should be
public String objectToSQLString(Object value, Dialect dialect)
otherwise the method is not overwritten correctly
Upvotes: 1
Reputation: 13465
The problem is with
createdate bigint(20) NULL
You should declare this as datetime
Try to
alter table table1 modify createdate datetime
Upvotes: 0