Skip to content

Commit e44b38f

Browse files
committed
HHH-8844 - Add support for Java 8 date and time types (JSR-310)
1 parent 5f6d1d2 commit e44b38f

14 files changed

+730
-12
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.type;
25+
26+
import java.time.Instant;
27+
import java.time.ZoneId;
28+
import java.time.ZonedDateTime;
29+
import java.time.format.DateTimeFormatter;
30+
import java.util.Comparator;
31+
import java.util.Locale;
32+
33+
import org.hibernate.dialect.Dialect;
34+
import org.hibernate.engine.spi.SessionImplementor;
35+
import org.hibernate.internal.util.compare.ComparableComparator;
36+
import org.hibernate.type.descriptor.java.InstantJavaDescriptor;
37+
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
38+
39+
/**
40+
* A type that maps between {@link java.sql.Types#TIMESTAMP TIMESTAMP} and {@link java.time.LocalDateTime}.
41+
*
42+
* @author Steve Ebersole
43+
*/
44+
public class InstantType
45+
extends AbstractSingleColumnStandardBasicType<Instant>
46+
implements VersionType<Instant>, LiteralType<Instant> {
47+
/**
48+
* Singleton access
49+
*/
50+
public static final InstantType INSTANCE = new InstantType();
51+
52+
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S 'Z'", Locale.ENGLISH );
53+
54+
public InstantType() {
55+
super( TimestampTypeDescriptor.INSTANCE, InstantJavaDescriptor.INSTANCE );
56+
}
57+
58+
@Override
59+
public String objectToSQLString(Instant value, Dialect dialect) throws Exception {
60+
return "{ts '" + FORMATTER.format( ZonedDateTime.ofInstant( value, ZoneId.of( "UTC" ) ) ) + "'}";
61+
}
62+
63+
@Override
64+
public Instant seed(SessionImplementor session) {
65+
return Instant.now();
66+
}
67+
68+
@Override
69+
public Instant next(Instant current, SessionImplementor session) {
70+
return Instant.now();
71+
}
72+
73+
@Override
74+
@SuppressWarnings("unchecked")
75+
public Comparator<Instant> getComparator() {
76+
return ComparableComparator.INSTANCE;
77+
}
78+
79+
@Override
80+
public String getName() {
81+
return "Instant";
82+
}
83+
84+
@Override
85+
protected boolean registerUnderJavaType() {
86+
return true;
87+
}
88+
}

hibernate-java8/src/main/java/org/hibernate/type/Java8DateTimeTypeContributor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,10 @@ public void contribute(TypeContributions typeContributions, ServiceRegistry serv
3838
typeContributions.contributeType( LocalDateTimeType.INSTANCE );
3939
typeContributions.contributeType( LocalDateType.INSTANCE );
4040
typeContributions.contributeType( LocalTimeType.INSTANCE );
41+
42+
typeContributions.contributeType( InstantType.INSTANCE );
43+
44+
typeContributions.contributeType( ZonedDateTimeType.INSTANCE );
45+
typeContributions.contributeType( OffsetDateTimeType.INSTANCE );
4146
}
4247
}

hibernate-java8/src/main/java/org/hibernate/type/LocalDateTimeType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.hibernate.dialect.Dialect;
3232
import org.hibernate.engine.spi.SessionImplementor;
3333
import org.hibernate.internal.util.compare.ComparableComparator;
34+
import org.hibernate.type.descriptor.java.LocalDateTimeJavaDescriptor;
3435
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
3536

3637
/**
@@ -46,7 +47,7 @@ public class LocalDateTimeType
4647
*/
4748
public static final LocalDateTimeType INSTANCE = new LocalDateTimeType();
4849

49-
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-mm-dd HH:mm:ss.S", Locale.ENGLISH );
50+
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S", Locale.ENGLISH );
5051

5152
public LocalDateTimeType() {
5253
super( TimestampTypeDescriptor.INSTANCE, LocalDateTimeJavaDescriptor.INSTANCE );

hibernate-java8/src/main/java/org/hibernate/type/LocalDateType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.hibernate.dialect.Dialect;
3232
import org.hibernate.engine.spi.SessionImplementor;
3333
import org.hibernate.internal.util.compare.ComparableComparator;
34+
import org.hibernate.type.descriptor.java.LocalDateJavaDescriptor;
3435
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
3536

3637
/**
@@ -45,7 +46,7 @@ public class LocalDateType
4546
*/
4647
public static final LocalDateType INSTANCE = new LocalDateType();
4748

48-
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-mm-dd", Locale.ENGLISH );
49+
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd", Locale.ENGLISH );
4950

5051
public LocalDateType() {
5152
super( TimestampTypeDescriptor.INSTANCE, LocalDateJavaDescriptor.INSTANCE );

hibernate-java8/src/main/java/org/hibernate/type/LocalTimeType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.hibernate.dialect.Dialect;
3232
import org.hibernate.engine.spi.SessionImplementor;
3333
import org.hibernate.internal.util.compare.ComparableComparator;
34+
import org.hibernate.type.descriptor.java.LocalTimeJavaDescriptor;
3435
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
3536

3637
/**
@@ -46,7 +47,7 @@ public class LocalTimeType
4647
*/
4748
public static final LocalTimeType INSTANCE = new LocalTimeType();
4849

49-
static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "HH:mm:ss", Locale.ENGLISH );
50+
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "HH:mm:ss", Locale.ENGLISH );
5051

5152
public LocalTimeType() {
5253
super( TimestampTypeDescriptor.INSTANCE, LocalTimeJavaDescriptor.INSTANCE );
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.type;
25+
26+
import java.time.OffsetDateTime;
27+
import java.time.ZonedDateTime;
28+
import java.time.format.DateTimeFormatter;
29+
import java.util.Comparator;
30+
import java.util.Locale;
31+
32+
import org.hibernate.dialect.Dialect;
33+
import org.hibernate.engine.spi.SessionImplementor;
34+
import org.hibernate.internal.util.compare.ComparableComparator;
35+
import org.hibernate.type.descriptor.java.OffsetDateTimeJavaDescriptor;
36+
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
37+
38+
/**
39+
* @author Steve Ebersole
40+
*/
41+
public class OffsetDateTimeType
42+
extends AbstractSingleColumnStandardBasicType<OffsetDateTime>
43+
implements VersionType<OffsetDateTime>, LiteralType<OffsetDateTime> {
44+
45+
/**
46+
* Singleton access
47+
*/
48+
public static final OffsetDateTimeType INSTANCE = new OffsetDateTimeType();
49+
50+
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S xxxxx", Locale.ENGLISH );
51+
52+
public OffsetDateTimeType() {
53+
super( TimestampTypeDescriptor.INSTANCE, OffsetDateTimeJavaDescriptor.INSTANCE );
54+
}
55+
56+
@Override
57+
public String objectToSQLString(OffsetDateTime value, Dialect dialect) throws Exception {
58+
return "{ts '" + FORMATTER.format( value ) + "'}";
59+
}
60+
61+
@Override
62+
public OffsetDateTime seed(SessionImplementor session) {
63+
return OffsetDateTime.now();
64+
}
65+
66+
@Override
67+
public OffsetDateTime next(OffsetDateTime current, SessionImplementor session) {
68+
return OffsetDateTime.now();
69+
}
70+
71+
@Override
72+
@SuppressWarnings("unchecked")
73+
public Comparator<OffsetDateTime> getComparator() {
74+
return ComparableComparator.INSTANCE;
75+
}
76+
77+
@Override
78+
public String getName() {
79+
return OffsetDateTime.class.getSimpleName();
80+
}
81+
82+
@Override
83+
protected boolean registerUnderJavaType() {
84+
return true;
85+
}
86+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* Copyright (c) 2015, Red Hat Inc. or third-party contributors as
5+
* indicated by the @author tags or express copyright attribution
6+
* statements applied by the authors. All third-party contributions are
7+
* distributed under license by Red Hat Inc.
8+
*
9+
* This copyrighted material is made available to anyone wishing to use, modify,
10+
* copy, or redistribute it subject to the terms and conditions of the GNU
11+
* Lesser General Public License, as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15+
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16+
* for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this distribution; if not, write to:
20+
* Free Software Foundation, Inc.
21+
* 51 Franklin Street, Fifth Floor
22+
* Boston, MA 02110-1301 USA
23+
*/
24+
package org.hibernate.type;
25+
26+
import java.time.ZonedDateTime;
27+
import java.time.format.DateTimeFormatter;
28+
import java.util.Comparator;
29+
import java.util.Locale;
30+
31+
import org.hibernate.dialect.Dialect;
32+
import org.hibernate.engine.spi.SessionImplementor;
33+
import org.hibernate.internal.util.compare.ComparableComparator;
34+
import org.hibernate.type.descriptor.java.ZonedDateTimeJavaDescriptor;
35+
import org.hibernate.type.descriptor.sql.TimestampTypeDescriptor;
36+
37+
/**
38+
* @author Steve Ebersole
39+
*/
40+
public class ZonedDateTimeType
41+
extends AbstractSingleColumnStandardBasicType<ZonedDateTime>
42+
implements VersionType<ZonedDateTime>, LiteralType<ZonedDateTime> {
43+
44+
/**
45+
* Singleton access
46+
*/
47+
public static final ZonedDateTimeType INSTANCE = new ZonedDateTimeType();
48+
49+
public static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern( "yyyy-MM-dd HH:mm:ss.S VV", Locale.ENGLISH );
50+
51+
public ZonedDateTimeType() {
52+
super( TimestampTypeDescriptor.INSTANCE, ZonedDateTimeJavaDescriptor.INSTANCE );
53+
}
54+
55+
@Override
56+
public String objectToSQLString(ZonedDateTime value, Dialect dialect) throws Exception {
57+
return "{ts '" + FORMATTER.format( value ) + "'}";
58+
}
59+
60+
@Override
61+
public ZonedDateTime seed(SessionImplementor session) {
62+
return ZonedDateTime.now();
63+
}
64+
65+
@Override
66+
public ZonedDateTime next(ZonedDateTime current, SessionImplementor session) {
67+
return ZonedDateTime.now();
68+
}
69+
70+
@Override
71+
@SuppressWarnings("unchecked")
72+
public Comparator<ZonedDateTime> getComparator() {
73+
return ComparableComparator.INSTANCE;
74+
}
75+
76+
@Override
77+
public String getName() {
78+
return ZonedDateTime.class.getSimpleName();
79+
}
80+
81+
@Override
82+
protected boolean registerUnderJavaType() {
83+
return true;
84+
}
85+
}

0 commit comments

Comments
 (0)