1

I have a table with a column type as TimeStamp in Mssql and generated pojos using hibernate(reverse engineering).In the generated Pojo,timestamp field is marked as byte[].

In the debugging process,I found TimeStamp sqlType code is "-2" that is Binary Type in Hibernate types and Hibernate binary type is equivalent to byte[] in java.

Java, hibernate and sql server for timestamp data type

 Mssql                 Hibernate             Java Type
 -----                 ---------             ---------

Timestamp               Binary                 byte[]

I dont understand why only mssql type TimeStamp is mapped with Binary in Hibernate.But in other databases like Postgres,oracle,mysql...timestamp is mapped to hibernate timestamp type.

I can resolve this issue by adding type-mapping in hibernate.reveng.xml by mapping sql timestamp to hibernate timestamp. Dont know what else issues occurs further.

Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38

2 Answers2

5

It seems MSSQL Timestamp is different from all other databases Timestamp type.

SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database. The timestamp data type was originally implemented to support the SQL Server recovery algorithms.

SQL Server TIMESTAMP value does not contain any date or time related value. It is not dependent on system date. In fact, it contains binary format string to denote a version of a row in table. That is why it is also called ROWVERSION. Also, ROWVERSION is the keyword in SQL Server which has the same behavior as TIMESTAMP.

Hence Hibernate is mapping sql server timestamp type to Binary type.

Sunil Kumar
  • 5,477
  • 4
  • 31
  • 38
2

The reason timestamp is binary, is because it is binary.

It is not a date/time; as some people might think.

If you want to store a date/time in SQL Server, you use:

  • datetime
  • datetime2
  • datetimeoffset
  • smalldatetime
  • date
  • time

timestamp in SQL Server is an alias of rowversion. rowversion is an 8-byte binary value (exposed to clients as binary(8)).

  • The confusion comes because you think timestamp refers to a date/time value - it does not.
  • timestamp is a constantly increasing counter; automatically incremented with every change on the row.

Timestamp is designed to be used with data synchronization and optimistic locking (as opposed to pessimistic locking that normally happens in databases).

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219