簡體   English   中英

ZonedDateTime 與 MongoDB

[英]ZonedDateTime with MongoDB

嘗試將ZonedDateTimeMongoDB一起使用。 我可以將ZonedDateTime保存在MongoDB但是當我查看記錄時,它有很多不必要的東西:

> "timestamp" : {
>             "dateTime" : ISODate("2016-12-13T13:45:53.991Z"),
>             "offset" : {
>                 "_id" : "-05:00",
>                 "totalSeconds" : -18000
>             },
>             "zone" : {
>                 "_class" : "java.time.ZoneRegion",
>                 "_id" : "America/New_York",
>                 "rules" : {
>                     "standardTransitions" : [ 
>                         NumberLong(-2717650800)
>                     ],
>                     "standardOffsets" : [ 
>                         {
>                             "_id" : "-04:56:02",
>                             "totalSeconds" : -17762
>                         }, 
>                         {
>                             "_id" : "-05:00",
>                             "totalSeconds" : -18000
>                         }
>                     ],
>                     "savingsInstantTransitions" : [ 
>                         NumberLong(-2717650800), 
>                         NumberLong(-1633280400), 
>                         NumberLong(-1615140000), 
>                         NumberLong(-1601830800), 
>                         NumberLong(-1583690400), 
>                         NumberLong(-1570381200),
> and so on....

此外,當我嘗試檢索相同的日期時,它給了我以下信息:

> org.springframework.data.mapping.model.MappingException: No property
> null found on entity class java.time.ZonedDateTime to bind constructor
> parameter to!

使用LocalDateTime時我沒有遇到這個問題。 第一個問題是我們可以在某處更改一些設置,這些設置只會保留ISODateZonedDateTime嗎? 第二個問題,是否有類似Jsr310JpaConverters for mongodb東西?

更新:參考以下問卷我創建了自定義轉換器並注冊了它們,但是,問題仍然存在。 帶有 Java 8 LocalDate MappingException 的 Spring Data MongoDB

public class ZonedDateTimeToLocalDateTimeConverter implements Converter<ZonedDateTime, LocalDateTime> {
    @Override
    public LocalDateTime convert(ZonedDateTime source) {
        return source == null ? null : LocalDateTime.ofInstant(source.toInstant(), ZoneId
                .systemDefault());
    }
}

public class LocalDateTimeToZonedDateTimeConverter implements Converter<LocalDateTime,
        ZonedDateTime> {
    @Override
    public ZonedDateTime convert(LocalDateTime source) {
        return source == null ? null : ZonedDateTime.of(source, ZoneId.systemDefault());
    }
}

注冊他們如下:

@Bean
public CustomConversions customConversions(){
        List<Converter<?,?>> converters = new ArrayList<Converter<?,?>>();
        converters.add(new ZonedDateTimeToLocalDateTimeConverter());
        converters.add(new LocalDateTimeToZonedDateTimeConverter());
        return new CustomConversions(converters);
    }

@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException {
        MappingMongoConverter converter = new MappingMongoConverter(
                new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext());
        converter.setCustomConversions(customConversions());
        converter.afterPropertiesSet();
        return new MongoTemplate(getMongoDbFactory(), converter);
    }

看起來 Spring 支持除ZonedDateTime轉換器之外的所有 java 時間轉換器。 您可以按如下方式注冊一個。

@Bean
public CustomConversions customConversions(){
    List<Converter<?,?>> converters = new ArrayList<>();
    converters.add(new DateToZonedDateTimeConverter());
    converters.add(new ZonedDateTimeToDateConverter());
    return new CustomConversions(converters);
}

@Bean
public MongoTemplate getMongoTemplate() throws UnknownHostException {
    MappingMongoConverter converter = new MappingMongoConverter(
            new DefaultDbRefResolver(getMongoDbFactory()), new MongoMappingContext());
    converter.setCustomConversions(customConversions());
    converter.afterPropertiesSet();
    return new MongoTemplate(getMongoDbFactory(), converter);
}
    
class DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {
    
     @Override
     public ZonedDateTime convert(Date source) {
              return source == null ? null : ofInstant(source.toInstant(), systemDefault());
         }
     }
    
class ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {
    
    @Override
    public Date convert(ZonedDateTime source) {
             return source == null ? null : Date.from(source.toInstant());
       }
   }

另一種替代解決方案是僅使用 ZonedDateTime 並將其更改為日期,同時保留到 MongoDB 中。 您可以在獲取時輕松地將其從日期更改回分區日期時間。

以下是幫助轉換的相關方法。

ZoneId zoneID = ZoneId.of("America/Chicago");

從 ZonedDateTime 到 java util 日期。

Instant instant = Instant.now();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);
Date date = Date.from(zdt.toInstant());

從日期到 ZonedDateTime

Instant instant = date.toInstant();
ZonedDateTime zonedDateTime = instant.atZone(zoneId);

另一種選擇是實現自定義編解碼器來幫助轉換。 我在從 Mongo 文檔過濾 YearMonth 時為 YearMonth 創建了一個。 如果讀者想要為 Zoned Date Time 創建自定義編解碼器,我會將其留給讀者作為練習。

您可以將以下庫用於基於編解碼器的方法。

https://github.com/ylemoigne/mongo-jackson-codec

在花了太多時間調試之后,我終於找到了最新版本的 spring boot / spring data 的工作解決方案。 這目前在 Spring Boot 2.0.0.M7 上對我有用。

使用 veeram 接受的答案,我得到了Couldn't find PersistentEntity for type

我希望這可以幫助人們避免陷入困境。

@Configuration
public class MongoConfiguration {

    @Bean
    public MongoCustomConversions customConversions(){
        List<Converter<?,?>> converters = new ArrayList<>();
        converters.add(DateToZonedDateTimeConverter.INSTANCE);
        converters.add( ZonedDateTimeToDateConverter.INSTANCE);
        return new MongoCustomConversions(converters);
    }

    enum DateToZonedDateTimeConverter implements Converter<Date, ZonedDateTime> {

        INSTANCE;

        @Override
        public ZonedDateTime convert(Date source) {
            return ofInstant(source.toInstant(), systemDefault());
        }
    }

    enum ZonedDateTimeToDateConverter implements Converter<ZonedDateTime, Date> {

        INSTANCE;

        @Override
        public Date convert(ZonedDateTime source) {
            return Date.from(source.toInstant());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM