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?
1 Answer
Reset to default 1Wrong 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
Instant
? The irony is that, afaik, the only 'instant type' in Postgres is timestamp with timezone or some such, which I thought maps toOffsetDateTime
naturally – g00se Commented Mar 23 at 21:06