HHH-8844 - Add support for Java 8 date and time types (JSR-310)
This commit is contained in:
parent
5f6d1d24f7
commit
e44b38f716
|
@ -0,0 +1,88 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Comparator;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.internal.util.compare.ComparableComparator;
|
||||
import org.hibernate.type.descriptor.java.InstantJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||
|
||||
/**
|
||||
* A type that maps between {@link java.sql.Types#TIMESTAMP TIMESTAMP} and {@link java.time.LocalDateTime}.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class InstantType
|
||||
extends AbstractSingleColumnStandardBasicType<Instant>
|
||||
implements VersionType<Instant>, LiteralType<Instant> {
|
||||
/**
|
||||
* Singleton access
|
||||
*/
|
||||
public static final InstantType INSTANCE = new InstantType();
|
||||
|
||||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S 'Z'", Locale.ENGLISH );
|
||||
|
||||
public InstantType() {
|
||||
super( TimestampTypeDescriptor.INSTANCE, InstantJavaDescriptor.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String objectToSQLString(Instant value, Dialect dialect) throws Exception {
|
||||
return "{ts '" + FORMATTER.format( ZonedDateTime.ofInstant( value, ZoneId.of( "UTC" ) ) ) + "'}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant seed(SessionImplementor session) {
|
||||
return Instant.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant next(Instant current, SessionImplementor session) {
|
||||
return Instant.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Comparator<Instant> getComparator() {
|
||||
return ComparableComparator.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Instant";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean registerUnderJavaType() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -38,5 +38,10 @@ public class Java8DateTimeTypeContributor implements TypeContributor {
|
|||
typeContributions.contributeType( LocalDateTimeType.INSTANCE );
|
||||
typeContributions.contributeType( LocalDateType.INSTANCE );
|
||||
typeContributions.contributeType( LocalTimeType.INSTANCE );
|
||||
|
||||
typeContributions.contributeType( InstantType.INSTANCE );
|
||||
|
||||
typeContributions.contributeType( ZonedDateTimeType.INSTANCE );
|
||||
typeContributions.contributeType( OffsetDateTimeType.INSTANCE );
|
||||
}
|
||||
}
|
|
@ -31,6 +31,7 @@ import java.util.Locale;
|
|||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.internal.util.compare.ComparableComparator;
|
||||
import org.hibernate.type.descriptor.java.LocalDateTimeJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||
|
||||
/**
|
||||
|
@ -46,7 +47,7 @@ public class LocalDateTimeType
|
|||
*/
|
||||
public static final LocalDateTimeType INSTANCE = new LocalDateTimeType();
|
||||
|
||||
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-mm-dd HH:mm:ss.S", Locale.ENGLISH );
|
||||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH );
|
||||
|
||||
public LocalDateTimeType() {
|
||||
super( TimestampTypeDescriptor.INSTANCE, LocalDateTimeJavaDescriptor.INSTANCE );
|
||||
|
|
|
@ -31,6 +31,7 @@ import java.util.Locale;
|
|||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.internal.util.compare.ComparableComparator;
|
||||
import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||
|
||||
/**
|
||||
|
@ -45,7 +46,7 @@ public class LocalDateType
|
|||
*/
|
||||
public static final LocalDateType INSTANCE = new LocalDateType();
|
||||
|
||||
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-mm-dd", Locale.ENGLISH );
|
||||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd", Locale.ENGLISH );
|
||||
|
||||
public LocalDateType() {
|
||||
super( TimestampTypeDescriptor.INSTANCE, LocalDateJavaDescriptor.INSTANCE );
|
||||
|
|
|
@ -31,6 +31,7 @@ import java.util.Locale;
|
|||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.internal.util.compare.ComparableComparator;
|
||||
import org.hibernate.type.descriptor.java.LocalTimeJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||
|
||||
/**
|
||||
|
@ -46,7 +47,7 @@ public class LocalTimeType
|
|||
*/
|
||||
public static final LocalTimeType INSTANCE = new LocalTimeType();
|
||||
|
||||
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "HH:mm:ss", Locale.ENGLISH );
|
||||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "HH:mm:ss", Locale.ENGLISH );
|
||||
|
||||
public LocalTimeType() {
|
||||
super( TimestampTypeDescriptor.INSTANCE, LocalTimeJavaDescriptor.INSTANCE );
|
||||
|
|
|
@ -0,0 +1,86 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Comparator;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.internal.util.compare.ComparableComparator;
|
||||
import org.hibernate.type.descriptor.java.OffsetDateTimeJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class OffsetDateTimeType
|
||||
extends AbstractSingleColumnStandardBasicType<OffsetDateTime>
|
||||
implements VersionType<OffsetDateTime>, LiteralType<OffsetDateTime> {
|
||||
|
||||
/**
|
||||
* Singleton access
|
||||
*/
|
||||
public static final OffsetDateTimeType INSTANCE = new OffsetDateTimeType();
|
||||
|
||||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S xxxxx", Locale.ENGLISH );
|
||||
|
||||
public OffsetDateTimeType() {
|
||||
super( TimestampTypeDescriptor.INSTANCE, OffsetDateTimeJavaDescriptor.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String objectToSQLString(OffsetDateTime value, Dialect dialect) throws Exception {
|
||||
return "{ts '" + FORMATTER.format( value ) + "'}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime seed(SessionImplementor session) {
|
||||
return OffsetDateTime.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime next(OffsetDateTime current, SessionImplementor session) {
|
||||
return OffsetDateTime.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Comparator<OffsetDateTime> getComparator() {
|
||||
return ComparableComparator.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return OffsetDateTime.class.getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean registerUnderJavaType() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Comparator;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.hibernate.dialect.Dialect;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.internal.util.compare.ComparableComparator;
|
||||
import org.hibernate.type.descriptor.java.ZonedDateTimeJavaDescriptor;
|
||||
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ZonedDateTimeType
|
||||
extends AbstractSingleColumnStandardBasicType<ZonedDateTime>
|
||||
implements VersionType<ZonedDateTime>, LiteralType<ZonedDateTime> {
|
||||
|
||||
/**
|
||||
* Singleton access
|
||||
*/
|
||||
public static final ZonedDateTimeType INSTANCE = new ZonedDateTimeType();
|
||||
|
||||
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S VV", Locale.ENGLISH );
|
||||
|
||||
public ZonedDateTimeType() {
|
||||
super( TimestampTypeDescriptor.INSTANCE, ZonedDateTimeJavaDescriptor.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String objectToSQLString(ZonedDateTime value, Dialect dialect) throws Exception {
|
||||
return "{ts '" + FORMATTER.format( value ) + "'}";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime seed(SessionImplementor session) {
|
||||
return ZonedDateTime.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime next(ZonedDateTime current, SessionImplementor session) {
|
||||
return ZonedDateTime.now();
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Comparator<ZonedDateTime> getComparator() {
|
||||
return ComparableComparator.INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return ZonedDateTime.class.getSimpleName();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean registerUnderJavaType() {
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,136 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type.descriptor.java;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.hibernate.type.InstantType;
|
||||
import org.hibernate.type.LocalDateType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
|
||||
/**
|
||||
* Java type descriptor for the LocalDateTime type.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class InstantJavaDescriptor extends AbstractTypeDescriptor<Instant> {
|
||||
/**
|
||||
* Singleton access
|
||||
*/
|
||||
public static final InstantJavaDescriptor INSTANCE = new InstantJavaDescriptor();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public InstantJavaDescriptor() {
|
||||
super( Instant.class, ImmutableMutabilityPlan.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(Instant value) {
|
||||
return InstantType.FORMATTER.format( ZonedDateTime.ofInstant( value, ZoneId.of( "UTC" ) ) );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Instant fromString(String string) {
|
||||
return (Instant) InstantType.FORMATTER.parse( string );
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <X> X unwrap(Instant instant, Class<X> type, WrapperOptions options) {
|
||||
if ( instant == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( Instant.class.isAssignableFrom( type ) ) {
|
||||
return (X) instant;
|
||||
}
|
||||
|
||||
if ( Calendar.class.isAssignableFrom( type ) ) {
|
||||
final ZoneId zoneId = ZoneId.ofOffset( "UTC", ZoneOffset.UTC );
|
||||
return (X) GregorianCalendar.from( instant.atZone( zoneId ) );
|
||||
}
|
||||
|
||||
if ( java.sql.Timestamp.class.isAssignableFrom( type ) ) {
|
||||
return (X) Timestamp.from( instant );
|
||||
}
|
||||
|
||||
if ( java.sql.Date.class.isAssignableFrom( type ) ) {
|
||||
return (X) java.sql.Date.from( instant );
|
||||
}
|
||||
|
||||
if ( java.sql.Time.class.isAssignableFrom( type ) ) {
|
||||
return (X) java.sql.Time.from( instant );
|
||||
}
|
||||
|
||||
if ( java.util.Date.class.isAssignableFrom( type ) ) {
|
||||
return (X) Date.from( instant );
|
||||
}
|
||||
|
||||
if ( Long.class.isAssignableFrom( type ) ) {
|
||||
return (X) Long.valueOf( instant.toEpochMilli() );
|
||||
}
|
||||
|
||||
throw unknownUnwrap( type );
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X> Instant wrap(X value, WrapperOptions options) {
|
||||
if ( value == null ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ( Instant.class.isInstance( value ) ) {
|
||||
return (Instant) value;
|
||||
}
|
||||
|
||||
if ( Timestamp.class.isInstance( value ) ) {
|
||||
final Timestamp ts = (Timestamp) value;
|
||||
return ts.toInstant();
|
||||
}
|
||||
|
||||
if ( Long.class.isInstance( value ) ) {
|
||||
return Instant.ofEpochMilli( (Long) value );
|
||||
}
|
||||
|
||||
if ( Calendar.class.isInstance( value ) ) {
|
||||
final Calendar calendar = (Calendar) value;
|
||||
return ZonedDateTime.ofInstant( calendar.toInstant(), calendar.getTimeZone().toZoneId() ).toInstant();
|
||||
}
|
||||
|
||||
if ( java.util.Date.class.isInstance( value ) ) {
|
||||
return ( (java.util.Date) value ).toInstant();
|
||||
}
|
||||
|
||||
throw unknownWrap( value.getClass() );
|
||||
}
|
||||
}
|
|
@ -21,7 +21,7 @@
|
|||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type;
|
||||
package org.hibernate.type.descriptor.java;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
|
@ -33,9 +33,8 @@ import java.util.Calendar;
|
|||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.hibernate.type.LocalDateType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
|
||||
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
||||
|
||||
/**
|
||||
* Java type descriptor for the LocalDateTime type.
|
|
@ -21,7 +21,7 @@
|
|||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type;
|
||||
package org.hibernate.type.descriptor.java;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
|
@ -31,9 +31,8 @@ import java.util.Calendar;
|
|||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.hibernate.type.LocalDateTimeType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
|
||||
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
||||
|
||||
/**
|
||||
* Java type descriptor for the LocalDateTime type.
|
|
@ -21,7 +21,7 @@
|
|||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type;
|
||||
package org.hibernate.type.descriptor.java;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
|
@ -34,9 +34,8 @@ import java.util.Calendar;
|
|||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.hibernate.type.LocalTimeType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
|
||||
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
|
||||
|
||||
/**
|
||||
* Java type descriptor for the LocalDateTime type.
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type.descriptor.java;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.hibernate.type.OffsetDateTimeType;
|
||||
import org.hibernate.type.ZonedDateTimeType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
|
||||
/**
|
||||
* Java type descriptor for the LocalDateTime type.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class OffsetDateTimeJavaDescriptor extends AbstractTypeDescriptor<OffsetDateTime> {
|
||||
/**
|
||||
* Singleton access
|
||||
*/
|
||||
public static final OffsetDateTimeJavaDescriptor INSTANCE = new OffsetDateTimeJavaDescriptor();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public OffsetDateTimeJavaDescriptor() {
|
||||
super( OffsetDateTime.class, ImmutableMutabilityPlan.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(OffsetDateTime value) {
|
||||
return OffsetDateTimeType.FORMATTER.format( value );
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetDateTime fromString(String string) {
|
||||
return (OffsetDateTime) OffsetDateTimeType.FORMATTER.parse( string );
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
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 ) ) {
|
||||
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 );
|
||||
}
|
||||
|
||||
@Override
|
||||
public <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;
|
||||
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() );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,134 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
|
||||
* indicated by the @author tags or express copyright attribution
|
||||
* statements applied by the authors. All third-party contributions are
|
||||
* distributed under license by Red Hat Inc.
|
||||
*
|
||||
* This copyrighted material is made available to anyone wishing to use, modify,
|
||||
* copy, or redistribute it subject to the terms and conditions of the GNU
|
||||
* Lesser General Public License, as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this distribution; if not, write to:
|
||||
* Free Software Foundation, Inc.
|
||||
* 51 Franklin Street, Fifth Floor
|
||||
* Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package org.hibernate.type.descriptor.java;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
import org.hibernate.type.InstantType;
|
||||
import org.hibernate.type.ZonedDateTimeType;
|
||||
import org.hibernate.type.descriptor.WrapperOptions;
|
||||
|
||||
/**
|
||||
* Java type descriptor for the LocalDateTime type.
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class ZonedDateTimeJavaDescriptor extends AbstractTypeDescriptor<ZonedDateTime> {
|
||||
/**
|
||||
* Singleton access
|
||||
*/
|
||||
public static final ZonedDateTimeJavaDescriptor INSTANCE = new ZonedDateTimeJavaDescriptor();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ZonedDateTimeJavaDescriptor() {
|
||||
super( ZonedDateTime.class, ImmutableMutabilityPlan.INSTANCE );
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString(ZonedDateTime value) {
|
||||
return ZonedDateTimeType.FORMATTER.format( value );
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime fromString(String string) {
|
||||
return (ZonedDateTime) ZonedDateTimeType.FORMATTER.parse( string );
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
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 ) ) {
|
||||
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 );
|
||||
}
|
||||
|
||||
@Override
|
||||
public <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;
|
||||
return ZonedDateTime.ofInstant( ts.toInstant(), 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() );
|
||||
}
|
||||
}
|
|
@ -23,9 +23,12 @@
|
|||
*/
|
||||
package org.hibernate.test.type;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.util.Iterator;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
@ -34,7 +37,9 @@ import javax.persistence.Table;
|
|||
import org.hibernate.Session;
|
||||
import org.hibernate.mapping.PersistentClass;
|
||||
import org.hibernate.mapping.Property;
|
||||
import org.hibernate.type.AbstractStandardBasicType;
|
||||
import org.hibernate.type.SerializableType;
|
||||
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
@ -74,12 +79,30 @@ public class Java8DateTimeTests extends BaseNonConfigCoreFunctionalTestCase {
|
|||
s = openSession();
|
||||
s.beginTransaction();
|
||||
theEntity = (TheEntity) s.get( TheEntity.class, 1 );
|
||||
dump( entityBinding, theEntity );
|
||||
assertNotNull( theEntity );
|
||||
s.delete( theEntity );
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
||||
private void dump(PersistentClass entityBinding, TheEntity theEntity) {
|
||||
final Iterator propertyBindingIterator = entityBinding.getPropertyClosureIterator();
|
||||
while ( propertyBindingIterator.hasNext() ) {
|
||||
final Property propertyBinding = (Property) propertyBindingIterator.next();
|
||||
final JavaTypeDescriptor javaTypeDescriptor = ( (AbstractStandardBasicType) propertyBinding.getType() ).getJavaTypeDescriptor();
|
||||
|
||||
System.out.println(
|
||||
String.format(
|
||||
"%s (%s) -> %s",
|
||||
propertyBinding.getName(),
|
||||
javaTypeDescriptor.getJavaTypeClass().getSimpleName(),
|
||||
javaTypeDescriptor.toString( propertyBinding.getGetter( TheEntity.class ).get( theEntity ) )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Entity(name = "TheEntity")
|
||||
@Table(name="the_entity")
|
||||
public static class TheEntity {
|
||||
|
@ -87,6 +110,9 @@ public class Java8DateTimeTests extends BaseNonConfigCoreFunctionalTestCase {
|
|||
private LocalDateTime localDateTime = LocalDateTime.now();
|
||||
private LocalDate localDate = LocalDate.now();
|
||||
private LocalTime localTime = LocalTime.now();
|
||||
private Instant instant = Instant.now();
|
||||
private ZonedDateTime zonedDateTime = ZonedDateTime.now();
|
||||
private OffsetDateTime offsetDateTime = OffsetDateTime.now();
|
||||
|
||||
public TheEntity() {
|
||||
}
|
||||
|
@ -127,5 +153,29 @@ public class Java8DateTimeTests extends BaseNonConfigCoreFunctionalTestCase {
|
|||
public void setLocalTime(LocalTime localTime) {
|
||||
this.localTime = localTime;
|
||||
}
|
||||
|
||||
public Instant getInstant() {
|
||||
return instant;
|
||||
}
|
||||
|
||||
public void setInstant(Instant instant) {
|
||||
this.instant = instant;
|
||||
}
|
||||
|
||||
public ZonedDateTime getZonedDateTime() {
|
||||
return zonedDateTime;
|
||||
}
|
||||
|
||||
public void setZonedDateTime(ZonedDateTime zonedDateTime) {
|
||||
this.zonedDateTime = zonedDateTime;
|
||||
}
|
||||
|
||||
public OffsetDateTime getOffsetDateTime() {
|
||||
return offsetDateTime;
|
||||
}
|
||||
|
||||
public void setOffsetDateTime(OffsetDateTime offsetDateTime) {
|
||||
this.offsetDateTime = offsetDateTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue