1

I must mapping a row_version column in hibernate, I have a SQL Server database and my table has a row_version column on DB this column is declared as timestamp because for SQL Server a rowversion is a special timestamp. How can I map this column in hibernate?

I tried to map this column as Timestamp as binary but it doesn't work.

  • Possible duplicate of [Hibernate timestamp version controlled by database.](https://stackoverflow.com/questions/12864826/hibernate-timestamp-version-controlled-by-database) – Amira Bedhiafi Jul 09 '19 at 09:49
  • What does "it doesn't work" mean? You got an error, unexpected behaviour, something *else*? – Thom A Jul 09 '19 at 09:54
  • I have already tried this solution https://stackoverflow.com/questions/12864826/hibernate-timestamp-version-controlled-by-database but I get tthe following error: Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: it.vwgroup.il.agenda.model.entity.EventEntity.rowVersion at – user5442782 Jul 09 '19 at 10:03
  • share your entity's code – Mansoor Ali Jul 09 '19 at 11:35
  • @Version @org.hibernate.annotations.Source(SourceType.DB) @org.hibernate.annotations.Generated(GenerationTime.ALWAYS) @Column(name = "ROW_VERSION") @Temporal(TemporalType.TIMESTAMP) private Date rowVersion; – user5442782 Jul 09 '19 at 11:40
  • are you using `java.util.Date` or `java.sql.Date` ? – Mansoor Ali Jul 09 '19 at 11:43
  • I have used java.sql.Date. – user5442782 Jul 09 '19 at 11:45
  • well, the exception that you are getting clearly says that `@Temporal should only be set on a java.util.Date or java.util.Calendar property` change `java.sql.Date` import to `java.util.Date` and it will work just fine – Mansoor Ali Jul 09 '19 at 11:47
  • ok but with java.uti.date I get the following error: Caused by: org.hibernate.HibernateException: Wrong column type in T_EVENT for column ROW_VERSION. Found: timestamp, expected: datetime – user5442782 Jul 09 '19 at 11:56
  • share schema of this table – Mansoor Ali Jul 09 '19 at 11:59
  • CREATE TABLE [T_EVENT]( [ID] [int] IDENTITY(1,1) NOT NULL, [ROW_VERSION] [timestamp] NULL, ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] – user5442782 Jul 09 '19 at 12:03
  • change type of `row_version` column to `datetime` instead of `timestamp` – Mansoor Ali Jul 09 '19 at 12:03
  • I'm sorry but this column is already used in the code, my row_version column doesn't contains a date, in the documentation the rowversion should be correctly declared "timestamp" https://learn.microsoft.com/en-us/sql/t-sql/data-types/rowversion-transact-sql?view=sql-server-2017 – user5442782 Jul 09 '19 at 12:17
  • I have used SQL Server 14.x – user5442782 Jul 09 '19 at 12:34

0 Answers0