Dropwizard - Postgresql - Hibernate 映射时间戳与 LocalDateTime 上的时区

Dropwizard - Postgresql - Hibernate mapping timestamp with time zone on LocalDateTime

我在 java.time.LocalDateTime 上映射 postgresql 类型 'timestamp with time zone' 时遇到问题。我正在使用:postgresql、带休眠功能的 dropwizard。

Table:

CREATE SEQUENCE "auth"."roles_seq_id";

CREATE TABLE "auth"."roles" (
  id        BIGINT                  NOT NULL    DEFAULT nextval('auth.roles_seq_id'),
  name      VARCHAR(50)             NOT NULL,
  createdat TIMESTAMP WITH TIME ZONE DEFAULT now() NOT NULL
);

ALTER TABLE "auth"."roles" ADD CONSTRAINT "roles_id_pk" PRIMARY KEY (id);

核心:

import lombok.*;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

@Getter
@Setter
@ToString(callSuper = true)
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(schema = "auth", name = "roles")
public class Role implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "roles_id_generator")
    @SequenceGenerator(name = "roles_id_generator", sequenceName = "roles_seq_id")
    @Column(name = "id")
    private Long id;

    @NonNull
    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "createdat")
    private LocalDateTime createdAt;

}

错误:

ERROR [2017-07-21 08:13:43,165] io.dropwizard.jersey.errors.LoggingExceptionMapper: Error handling a request: b09a66b06dd214bd ... ! Causing: org.hibernate.type.SerializationException: could not deserialize

POM.XML:

<dropwizard.version>1.0.6</dropwizard.version>
<postgresql.version>9.4.1212</postgresql.version>

 <dependency>
            <groupId>io.dropwizard</groupId>
            <artifactId>dropwizard-hibernate</artifactId>
            <version>${dropwizard.version}</version>
 </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>${postgresql.version}</version>
  </dependency>

当我删除createdAt或使用private Date createdAt(但我想使用LocalDateTime)时,一切正常。

看来 dropwizard-hibernate:1.0.6 捆绑了 Hibernate 5.1。根据 the manual 需要额外的依赖项来支持 java.time 类型:

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

此外,从 Hibernate 5.2 开始,不需要这个额外的依赖项,似乎功能已折叠到主要依赖项中。