Created
August 1, 2014 17:38
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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