2017-03-13 41 views
0

简化的例子不能在NativeQuery反序列化的DateTime与Java /休眠

我在它运行与日期本机查询:

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; 
    } 

}

当我在我的控制器执行该代码,我得到以下异常:

{ 
    "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] 

当我改变了日期时间转换成String在OverviewEvent类和SqlResultSetMapping中,它的工作原理。但我想使用日期时间格式。

有没有人可以帮助我解决这个问题。我尝试了不同的东西,例如:

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

但是例外保持不变。谢谢你的协助。

回答

1

尝试使用的Date(java.util.Date)代替DateTime

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

或创建日期时间自定义德/串。

+0

谢谢。今天晚上我再次参与这个项目时,我会试试。因为我是一个新手,这是你在说什么: http://www.baeldung.com/jackson-custom-serialization http://www.baeldung.com/jackson-deserialization – schingeldi

+0

工作。非常感谢 – schingeldi