嗎啡依賴於默認的構造函數時映射實體從MongoDB中到Java對象,java.time.LocalDateTime類沒有它引發異常。
您可以使用@PreLoad生命週期註解數據如下轉換:
@Entity
public class MyEntity {
public MyEntity() {
}
@Id
private ObjectId id;
private LocalDateTime localDateTime;
@PreLoad
public void preLoad(DBObject obj) {
BasicDBObject object = (BasicDBObject) obj.get("localDateTime");
BasicDBObject date = (BasicDBObject) object.get("date");
BasicDBObject time = (BasicDBObject) object.get("time");
localDateTime = LocalDateTime.of(date.getInt("year"),
date.getInt("month"),
date.getInt("day"),
time.getInt("hour"),
time.getInt("minute"),
time.getInt("second"),
time.getInt("nano"));
obj.removeField("localDateTime");
}
}