Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Created August 1, 2014 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsheets/3b9207cb5a5ac61ca913 to your computer and use it in GitHub Desktop.
Save jeffsheets/3b9207cb5a5ac61ca913 to your computer and use it in GitHub Desktop.
Hibernate Custom Type to use @Version optimistic locking for SQLServer datetime fields that are limited by 1/300th of a second precision
public class SqlServerTimestampType extends TimestampType {
private static final int SQLSERVER_PRECISION = 10;
public SqlServerTimestampType() {
super();
}
/**
* SQLServer datetime fields are accurate to 1/300th of a second.
* We floor to the nearest 1/100th of a second for simplicity.
* @see http://msdn.microsoft.com/en-us/library/aa258277%28SQL.80%29.aspx
*/
@Override
public Date seed(SessionImplementor session) {
return new Timestamp( roundToSqlServerPrecision(System.currentTimeMillis()) );
}
/**
* This will floor a long value to the nearest 10th place
* @param value to floor to closest 10
* @return long floored to closest 10
*/
public long roundToSqlServerPrecision(long value)
{
return (value / SQLSERVER_PRECISION) * SQLSERVER_PRECISION;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment