hibernate - Cannot convert unsupported date type java.time.LocalDateTime to java.time.OffsetDateTime - Stack Overflow

I have this Entity used by Spring Data JPA and Hibernate. I use Spring Cloud 2024.0.0 with PostgreSQL.

I have this Entity used by Spring Data JPA and Hibernate. I use Spring Cloud 2024.0.0 with PostgreSQL.

@Table(name = "errors")
public class UserError {

  @Id
  private Integer id;

  @Column(name = "created_by", insertable = true, updatable = false, nullable = false)
  @CreatedBy
  private String createdBy;

  @Column(name = "created_date", insertable = true, updatable = false, nullable = false)
  @CreatedDate
  private OffsetDateTime createdDate;

}

When I try to insert a new record I get error:

.springframework.dao.InvalidDataAccessApiUsageException: Cannot convert unsupported date type java.time.LocalDateTime to java.time.OffsetDateTime; Supported types are [java.time.LocalDateTime, java.time.LocalDate, java.time.LocalTime, java.time.Instant, java.util.Date, java.lang.Long, long]
    at .springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371) ~[spring-orm-6.2.0.jar:6.2.0]
    at .springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246) ~[spring-orm-6.2.0.jar:6.2.0]
    at .springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:560) ~[spring-orm-6.2.0.jar:6.2.0]
    at .springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-6.2.0.jar:6.2.0]
    at .springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:343) ~[spring-tx-6.2.0.jar:6.2.0]
    at .springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:160) ~[spring-tx-6.2.0.jar:6.2.0]
    at .springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.2.0.jar:6.2.0]
    at .springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:165) ~[spring-data-jpa-3.4.0.jar:3.4.0]

I don't have a filed with type java.time.LocalDateTime into my code. Do you know what might be the issue?

I have this Entity used by Spring Data JPA and Hibernate. I use Spring Cloud 2024.0.0 with PostgreSQL.

@Table(name = "errors")
public class UserError {

  @Id
  private Integer id;

  @Column(name = "created_by", insertable = true, updatable = false, nullable = false)
  @CreatedBy
  private String createdBy;

  @Column(name = "created_date", insertable = true, updatable = false, nullable = false)
  @CreatedDate
  private OffsetDateTime createdDate;

}

When I try to insert a new record I get error:

.springframework.dao.InvalidDataAccessApiUsageException: Cannot convert unsupported date type java.time.LocalDateTime to java.time.OffsetDateTime; Supported types are [java.time.LocalDateTime, java.time.LocalDate, java.time.LocalTime, java.time.Instant, java.util.Date, java.lang.Long, long]
    at .springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371) ~[spring-orm-6.2.0.jar:6.2.0]
    at .springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246) ~[spring-orm-6.2.0.jar:6.2.0]
    at .springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:560) ~[spring-orm-6.2.0.jar:6.2.0]
    at .springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-6.2.0.jar:6.2.0]
    at .springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:343) ~[spring-tx-6.2.0.jar:6.2.0]
    at .springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:160) ~[spring-tx-6.2.0.jar:6.2.0]
    at .springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.2.0.jar:6.2.0]
    at .springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:165) ~[spring-data-jpa-3.4.0.jar:3.4.0]

I don't have a filed with type java.time.LocalDateTime into my code. Do you know what might be the issue?

Share Improve this question asked Mar 23 at 20:58 Peter PenzovPeter Penzov 1,608156 gold badges500 silver badges907 bronze badges 3
  • 1 The error message tells you which classes are supported. What is unclear? – Just another Java programmer Commented Mar 23 at 21:02
  • In my other projects I use the same tech stack. I don't get the same error. – Peter Penzov Commented Mar 23 at 21:04
  • Make createdDate Instant? The irony is that, afaik, the only 'instant type' in Postgres is timestamp with timezone or some such, which I thought maps to OffsetDateTime naturally – g00se Commented Mar 23 at 21:06
Add a comment  | 

1 Answer 1

Reset to default 1

Wrong type on database column

I guess that your created_date column in your database table was mistakenly defined as type TIMESTAMP WITHOUT TIME ZONE rather than TIMESTAMP WITH TIME ZONE.

➡️ You’ll need to refactor your database to use the correct type.

TIMESTAMP WITH TIME ZONE maps to java.time.OffsetDateTime in JDBC 4.2 and later. This type represents a moment, a point on the time line.

TIMESTAMP WITHOUT TIME ZONE maps to java.time.LocalDateTime in JDBC 4.2 and later. This type does not represent a moment, is not a point on the timeline. This type is inherently ambiguous with regard to the timeline. Never use this type to record when something happened such as when a record was created.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744269304a4566020.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信