Peter Penzov
Peter Penzov

Reputation: 1754

Create column type Datetime in Hibernate for LocalDateTime

What is the proper way to create column type Datetime in MariaDB using JPA? I tried this:

@Column
@Temporal(TemporalType.TIMESTAMP)
private LocalDateTime created_at;

But I get exception:

Caused by: org.hibernate.AnnotationException: @Temporal should only be set on a java.util.Date or java.util.Calendar property: org.plugin.entity.Transactions.created_at

Can you propose some solution?

Upvotes: 1

Views: 1071

Answers (2)

senjin.hajrulahovic
senjin.hajrulahovic

Reputation: 3191

You have the spring-boot-starter-data-jpa dependency which has hibernate-core as a compile dependency.

If you are using spring boot 1.4.x or higher you get Hibernate 5:

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa/1.4.0.RELEASE

The you just have to add the following dependency:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.0.Final</version>
</dependency>

which provides support for Java 8 LocalDateTime API.

If you are using Spring boot version 2.x.x or higher spring-boot-starter-data-jpa ships with hibernate 5.2 which has Java 8 LocalDateTime API build in so no additional dependencies are necessary.

You can simply write:

private LocalDateTime created_at;

Upvotes: 2

drowny
drowny

Reputation: 2147

Update version of Hibernate to 5.x or latest, there is

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>5.0.0.Final</version>
</dependency>

You don't have to do anything else. Just add the dependency. Of course other Java8 types should also work.

private LocalDateTime created_at;

Upvotes: 0

Related Questions