简化示例
我运行一个包含日期的 native 查询:
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 类看起来像:
@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;
}
}
当我在 Controller 中执行此代码时,出现以下异常:
{
"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 类和 SqlResultSetMapping 中将 DateTime 更改为 String 时,它起作用了。但我想使用日期时间格式。
有谁可以帮助我解决这个异常(exception)吗?我尝试了不同的事情,例如:
@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 创建自定义反/序列化器。
关于java - 无法使用 Java/Hibernate 反序列化 NativeQuery 中的 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42773811/