龙空技术网

Hibernate如何映射 java 8 DateTime

jacob的vlog 247

前言:

目前同学们对“什么是映射java”大概比较看重,兄弟们都想要知道一些“什么是映射java”的相关内容。那么小编也在网摘上收集了一些关于“什么是映射java””的相关内容,希望咱们能喜欢,小伙伴们一起来学习一下吧!

Hibernate映射LocalDateTime, ZonedDateTime, OffsetDateTime三种类型值到数据库

1 LocalDateTime

LocalDateTimeType -> LocalDateTimeJavaDescriptor

public <X> X unwrap(LocalDateTime value, Class<X> type, WrapperOptions options) {		if ( value == null ) {			return null;		}		if ( LocalDateTime.class.isAssignableFrom( type ) ) {			return (X) value;		}		if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {			/*			 * Workaround for HHH-13266 (JDK-8061577).			 * We used to do Timestamp.from( value.atZone( ZoneId.systemDefault() ).toInstant() ),			 * but on top of being more complex than the line below, it won't always work.			 * Timestamp.from() assumes the number of milliseconds since the epoch			 * means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.			 */			return (X) Timestamp.valueOf( value );		}		if ( java.sql.Date.class.isAssignableFrom( type ) ) {			Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();			return (X) java.sql.Date.from( instant );		}		if ( java.sql.Time.class.isAssignableFrom( type ) ) {			Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();			return (X) java.sql.Time.from( instant );		}		if ( java.util.Date.class.isAssignableFrom( type ) ) {			Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();			return (X) java.util.Date.from( instant );		}		if ( Calendar.class.isAssignableFrom( type ) ) {			return (X) GregorianCalendar.from( value.atZone( ZoneId.systemDefault() ) );		}		if ( Long.class.isAssignableFrom( type ) ) {			Instant instant = value.atZone( ZoneId.systemDefault() ).toInstant();			return (X) Long.valueOf( instant.toEpochMilli() );		}		throw unknownUnwrap( type );	}	@Override	public <X> LocalDateTime wrap(X value, WrapperOptions options) {		if ( value == null ) {			return null;		}		if ( LocalDateTime.class.isInstance( value ) ) {			return (LocalDateTime) value;		}		if ( Timestamp.class.isInstance( value ) ) {			final Timestamp ts = (Timestamp) value;			/*			 * Workaround for HHH-13266 (JDK-8061577).			 * We used to do LocalDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() ),			 * but on top of being more complex than the line below, it won't always work.			 * ts.toInstant() assumes the number of milliseconds since the epoch			 * means the same thing in Timestamp and Instant, but it doesn't, in particular before 1900.			 */			return ts.toLocalDateTime();		}		if ( Long.class.isInstance( value ) ) {			final Instant instant = Instant.ofEpochMilli( (Long) value );			return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );		}		if ( Calendar.class.isInstance( value ) ) {			final Calendar calendar = (Calendar) value;			return LocalDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );		}		if ( Date.class.isInstance( value ) ) {			final Timestamp ts = (Timestamp) value;			final Instant instant = Instant.ofEpochMilli( ts.getTime() );			return LocalDateTime.ofInstant( instant, ZoneId.systemDefault() );		}		throw unknownWrap( value.getClass() );	}
2 ZonedDateTime

ZonedDateTimeType-> ZonedDateTimeJavaDescriptor

public <X> X unwrap(ZonedDateTime zonedDateTime, Class<X> type, WrapperOptions options) {   if ( zonedDateTime == null ) {      return null;   }   if ( ZonedDateTime.class.isAssignableFrom( type ) ) {      return (X) zonedDateTime;   }   if ( Calendar.class.isAssignableFrom( type ) ) {      return (X) GregorianCalendar.from( zonedDateTime );   }   if ( Timestamp.class.isAssignableFrom( type ) ) {      /*       * This works around two bugs:       * - HHH-13266 (JDK-8061577): around and before 1900,       * the number of milliseconds since the epoch does not mean the same thing       * for java.util and java.time, so conversion must be done using the year, month, day, hour, etc.       * - HHH-13379 (JDK-4312621): after 1908 (approximately),       * Daylight Saving Time introduces ambiguity in the year/month/day/hour/etc representation once a year       * (on DST end), so conversion must be done using the number of milliseconds since the epoch.       * - around 1905, both methods are equally valid, so we don't really care which one is used.       */      if ( zonedDateTime.getYear() < 1905 ) {         return (X) Timestamp.valueOf(               zonedDateTime.withZoneSameInstant( ZoneId.systemDefault() ).toLocalDateTime()         );      }      else {         return (X) Timestamp.from( zonedDateTime.toInstant() );      }   }   if ( java.sql.Date.class.isAssignableFrom( type ) ) {      return (X) java.sql.Date.from( zonedDateTime.toInstant() );   }   if ( java.sql.Time.class.isAssignableFrom( type ) ) {      return (X) java.sql.Time.from( zonedDateTime.toInstant() );   }   if ( Date.class.isAssignableFrom( type ) ) {      return (X) Date.from( zonedDateTime.toInstant() );   }   if ( Long.class.isAssignableFrom( type ) ) {      return (X) Long.valueOf( zonedDateTime.toInstant().toEpochMilli() );   }   throw unknownUnwrap( type );}@Overridepublic <X> ZonedDateTime wrap(X value, WrapperOptions options) {   if ( value == null ) {      return null;   }   if ( ZonedDateTime.class.isInstance( value ) ) {      return (ZonedDateTime) value;   }   if ( java.sql.Timestamp.class.isInstance( value ) ) {      final Timestamp ts = (Timestamp) value;      /*       * This works around two bugs:       * - HHH-13266 (JDK-8061577): around and before 1900,       * the number of milliseconds since the epoch does not mean the same thing       * for java.util and java.time, so conversion must be done using the year, month, day, hour, etc.       * - HHH-13379 (JDK-4312621): after 1908 (approximately),       * Daylight Saving Time introduces ambiguity in the year/month/day/hour/etc representation once a year       * (on DST end), so conversion must be done using the number of milliseconds since the epoch.       * - around 1905, both methods are equally valid, so we don't really care which one is used.       */      if ( ts.getYear() < 5 ) { // Timestamp year 0 is 1900         return ts.toLocalDateTime().atZone( ZoneId.systemDefault() );      }      else {         return ts.toInstant().atZone( ZoneId.systemDefault() );      }   }   if ( java.util.Date.class.isInstance( value ) ) {      final java.util.Date date = (java.util.Date) value;      return ZonedDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );   }   if ( Long.class.isInstance( value ) ) {      return ZonedDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() );   }   if ( Calendar.class.isInstance( value ) ) {      final Calendar calendar = (Calendar) value;      return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );   }   throw unknownWrap( value.getClass() );}
3 OffsetDateTime

OffsetDateTimeType -> OffsetDateTimeJavaDescriptor

public <X> X unwrap(OffsetDateTime offsetDateTime, Class<X> type, WrapperOptions options) {   if ( offsetDateTime == null ) {      return null;   }   if ( OffsetDateTime.class.isAssignableFrom( type ) ) {      return (X) offsetDateTime;   }   if ( Calendar.class.isAssignableFrom( type ) ) {      return (X) GregorianCalendar.from( offsetDateTime.toZonedDateTime() );   }   if ( Timestamp.class.isAssignableFrom( type ) ) {      /*       * This works around two bugs:       * - HHH-13266 (JDK-8061577): around and before 1900,       * the number of milliseconds since the epoch does not mean the same thing       * for java.util and java.time, so conversion must be done using the year, month, day, hour, etc.       * - HHH-13379 (JDK-4312621): after 1908 (approximately),       * Daylight Saving Time introduces ambiguity in the year/month/day/hour/etc representation once a year       * (on DST end), so conversion must be done using the number of milliseconds since the epoch.       * - around 1905, both methods are equally valid, so we don't really care which one is used.       */      if ( offsetDateTime.getYear() < 1905 ) {         return (X) Timestamp.valueOf(               offsetDateTime.atZoneSameInstant( ZoneId.systemDefault() ).toLocalDateTime()         );      }      else {         return (X) Timestamp.from( offsetDateTime.toInstant() );      }   }   if ( java.sql.Date.class.isAssignableFrom( type ) ) {      return (X) java.sql.Date.from( offsetDateTime.toInstant() );   }   if ( java.sql.Time.class.isAssignableFrom( type ) ) {      return (X) java.sql.Time.from( offsetDateTime.toInstant() );   }   if ( Date.class.isAssignableFrom( type ) ) {      return (X) Date.from( offsetDateTime.toInstant() );   }   if ( Long.class.isAssignableFrom( type ) ) {      return (X) Long.valueOf( offsetDateTime.toInstant().toEpochMilli() );   }   throw unknownUnwrap( type );}@Overridepublic <X> OffsetDateTime wrap(X value, WrapperOptions options) {   if ( value == null ) {      return null;   }   if ( OffsetDateTime.class.isInstance( value ) ) {      return (OffsetDateTime) value;   }   if ( Timestamp.class.isInstance( value ) ) {      final Timestamp ts = (Timestamp) value;      /*       * This works around two bugs:       * - HHH-13266 (JDK-8061577): around and before 1900,       * the number of milliseconds since the epoch does not mean the same thing       * for java.util and java.time, so conversion must be done using the year, month, day, hour, etc.       * - HHH-13379 (JDK-4312621): after 1908 (approximately),       * Daylight Saving Time introduces ambiguity in the year/month/day/hour/etc representation once a year       * (on DST end), so conversion must be done using the number of milliseconds since the epoch.       * - around 1905, both methods are equally valid, so we don't really care which one is used.       */      if ( ts.getYear() < 5 ) { // Timestamp year 0 is 1900         return ts.toLocalDateTime().atZone( ZoneId.systemDefault() ).toOffsetDateTime();      }      else {         return OffsetDateTime.ofInstant( ts.toInstant(), ZoneId.systemDefault() );      }   }   if ( Date.class.isInstance( value ) ) {      final Date date = (Date) value;      return OffsetDateTime.ofInstant( date.toInstant(), ZoneId.systemDefault() );   }   if ( Long.class.isInstance( value ) ) {      return OffsetDateTime.ofInstant( Instant.ofEpochMilli( (Long) value ), ZoneId.systemDefault() );   }   if ( Calendar.class.isInstance( value ) ) {      final Calendar calendar = (Calendar) value;      return OffsetDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() );   }   throw unknownWrap( value.getClass() );}

标签: #什么是映射java #java引入jacob #数据库中的datetimejava用什么接