无法使用 Java / Hibernate 在 NativeQuery 中反序列化 DateTime

Can't deserialize DateTime in NativeQuery with Java / Hibernate

简化示例

我 运行 包含日期的本地查询:

SELECT id, start FROM event;

我做了一个像这样的 SqlResultSetMapping:

@SqlResultSetMapping(
        name="EventMapping",
        classes={
                @ConstructorResult(
                        targetClass=de.teamsystems.domain.OverviewEvent.class,
                        columns={
                                @ColumnResult(name="id", type = Long.class ),    
                                @ColumnResult(name="start", type = DateTime.class )

                        }
                )
        }
)

我的 OverviewEvent class 看起来像:

@Entity
public class OverviewEvent  {

    @Id
    private Long id;

    private String name;


    private DateTime start;

    public Long getId() {
        return id;
    }

    public DateTime getStart() {
        return start;
    }

    public OverviewEvent(Long id, DateTime start) {
        this.id = id;
        this.start = start;
    }

}

当我在控制器中执行此代码时,出现以下异常:

{
    "error": "Internal Server Error",
    "exception": "javax.persistence.PersistenceException",
    "message": "org.hibernate.type.SerializationException: could not deserialize",
    "path": "/event",
    "status": 500,
    "timestamp": "2017-03-13T22:22:30.527+0100"
}

日志文件说:

2017-03-13 22:22:30.523 ERROR 23479 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize] with root cause

java.io.StreamCorruptedException: invalid stream header: 32303137
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:808) ~[na:1.8.0_111]

当我在 OverviewEvent class 和 SqlResultSetMapping 中将 DateTime 更改为 String 时,它起作用了。但我想使用日期时间格式。

有没有人能帮我解决这个例外。我尝试了不同的东西,例如:

@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
private DateTime start;

但异常保持不变。谢谢你帮我。

尝试使用 Date (java.util.Date) 而不是 DateTime:

  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
  private Date start;

或为 DateTime 创建自定义 de/serializer。