Martijn Burger
Martijn Burger

Reputation: 7543

Using LocalDateTime with @Version annotation in JPA

I have the following fields in my Entity class:

@Version
@Column(name = "updated")
private LocalDateTime lastUpdated;

@Column(name = "created")
private LocalDateTime created;

The created field works fine, because I implemented an AttributeConverter from LocalDateTime to Timestamp.

The lastUpdated field gives the following error when deploying to the container:

java.lang.ClassCastException: org.hibernate.type.descriptor.converter.AttributeConverterTypeAdapter cannot be cast to org.hibernate.type.VersionType

I know this can be solved by using the Timestamp as the variable type of lastUpdated instead of LocalDateTime.

I want to know if I can override the @Version annotation behavior to accept the LocalDateTime as a variable type.

Upvotes: 1

Views: 2680

Answers (1)

Saravana
Saravana

Reputation: 12817

Try hibernate-java8 dependency which is to support java 8 specific data types.

<!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-java8 -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.1.0.Final</version>
</dependency>

Below from hibernate website

Support for using Java8 specific data-types such as any of the JSR 310 Date/Time types in domain model.

VersionType<Instant> is added as part of this, so you could use this for your requirement

UPDATE 1

Checked Version annotation on LocalDateTime with hibernate-java8 dependency on H2 DB, it worked

Upvotes: 3

Related Questions