【问题标题】:Dropwizard - Postgresql - Hibernate mapping timestamp with time zone on LocalDateTimeDropwizard - Postgresql - 休眠映射时间戳与 LocalDateTime 上的时区
【发布时间】:2017-12-27 05:04:15
【问题描述】:

我在 java.time.LocalDateTime 上映射 postgresql 类型“带时区的时间戳”时遇到问题。我正在使用:postgresql、dropwizard with hibernate。

表:

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;

}

错误:

错误 [2017-07-21 08:13:43,165] io.dropwizard.jersey.errors.LoggingExceptionMapper:错误处理 请求:b09a66b06dd214bd ...!导致: org.hibernate.type.SerializationException: 无法反序列化

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)时,一切正常。

【问题讨论】:

  • 你使用的是什么版本的 Hibernate?
  • 已添加到帖子中
  • 快速浏览一下,Dropwizard 似乎在使用 Hibernate v5.1.0,您是否需要 hibernate-java8 依赖项?
  • org.hibernatehibernate-java85.1.0.Final 我用过这个代码和工作。谢谢。
  • 哦,太好了,我将其正式化为未来访问者的答案。

标签: java postgresql hibernate hibernate-mapping dropwizard


【解决方案1】:

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 开始,不需要这种额外的依赖项,似乎该功能已折叠到主要依赖项中。

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 2020-05-15
    • 2017-05-17
    • 2013-03-27
    • 1970-01-01
    • 2018-09-28
    • 2023-03-29
    • 2019-11-17
    相关资源
    最近更新 更多