1 package com.vladmihalcea.hibernate.type;
2
3 import com.vladmihalcea.hibernate.type.array.*;
4 import com.vladmihalcea.hibernate.type.basic.*;
5 import com.vladmihalcea.hibernate.type.interval.OracleIntervalDayToSecondType;
6 import com.vladmihalcea.hibernate.type.interval.PostgreSQLIntervalType;
7 import com.vladmihalcea.hibernate.type.interval.PostgreSQLPeriodType;
8 import com.vladmihalcea.hibernate.type.json.*;
9 import com.vladmihalcea.hibernate.type.money.CurrencyUnitType;
10 import com.vladmihalcea.hibernate.type.money.MonetaryAmountType;
11 import com.vladmihalcea.hibernate.type.range.PostgreSQLRangeType;
12 import com.vladmihalcea.hibernate.type.range.guava.PostgreSQLGuavaRangeType;
13 import com.vladmihalcea.hibernate.type.search.PostgreSQLTSVectorType;
14 import com.vladmihalcea.hibernate.util.ReflectionUtils;
15 import org.hibernate.boot.model.TypeContributions;
16 import org.hibernate.boot.model.TypeContributor;
17 import org.hibernate.dialect.*;
18 import org.hibernate.engine.jdbc.spi.JdbcServices;
19 import org.hibernate.service.ServiceRegistry;
20 import org.hibernate.type.BasicType;
21 import org.hibernate.usertype.CompositeUserType;
22 import org.hibernate.usertype.UserType;
23
24 /**
25  * The {@link HibernateTypesContributor} registers various types automatically.
26  *
27  * @author Vlad Mihalcea
28  * @since 2.15.0
29  */

30 public class HibernateTypesContributor implements TypeContributor {
31
32     @Override
33     public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
34         JdbcServices jdbcServices = serviceRegistry.getService(JdbcServices.class);
35         Dialect dialect = jdbcServices.getDialect();
36
37         boolean enableJson = ReflectionUtils.getClassOrNull("com.fasterxml.jackson.databind.ObjectMapper") != null;
38
39         if(dialect instanceof PostgreSQL82Dialect) {
40             /* Arrays */
41             this
42             .contributeType(typeContributions, BooleanArrayType.INSTANCE)
43             .contributeType(typeContributions, DateArrayType.INSTANCE)
44             .contributeType(typeContributions, DecimalArrayType.INSTANCE)
45             .contributeType(typeContributions, DoubleArrayType.INSTANCE)
46             .contributeType(typeContributions, EnumArrayType.INSTANCE)
47             .contributeType(typeContributions, IntArrayType.INSTANCE)
48             .contributeType(typeContributions, ListArrayType.INSTANCE)
49             .contributeType(typeContributions, LocalDateArrayType.INSTANCE)
50             .contributeType(typeContributions, LocalDateTimeArrayType.INSTANCE)
51             .contributeType(typeContributions, LongArrayType.INSTANCE)
52             .contributeType(typeContributions, StringArrayType.INSTANCE)
53             .contributeType(typeContributions, TimestampArrayType.INSTANCE)
54             .contributeType(typeContributions, UUIDArrayType.INSTANCE)
55             /* Date/Time */
56             .contributeType(typeContributions, PostgreSQLIntervalType.INSTANCE)
57             .contributeType(typeContributions, PostgreSQLPeriodType.INSTANCE)
58             /* Specific-types */
59             .contributeType(typeContributions, PostgreSQLTSVectorType.INSTANCE)
60             .contributeType(typeContributions, PostgreSQLEnumType.INSTANCE)
61             .contributeType(typeContributions, PostgreSQLHStoreType.INSTANCE)
62             .contributeType(typeContributions, PostgreSQLInetType.INSTANCE)
63             .contributeType(typeContributions, PostgreSQLRangeType.INSTANCE)
64             .contributeType(typeContributions, PostgreSQLCITextType.INSTANCE);
65
66             if(ReflectionUtils.getClassOrNull("com.google.common.collect.Range") != null) {
67                 this.contributeType(typeContributions, PostgreSQLGuavaRangeType.INSTANCE);
68             }
69             if(enableJson) {
70                 /* JSON */
71                 this.contributeType(typeContributions, JsonBinaryType.INSTANCE);
72             }
73         } else if(dialect instanceof MySQLDialect) {
74             /* JSON */
75             if (enableJson) {
76                 this.contributeType(typeContributions, JsonStringType.INSTANCE)
77                     .contributeType(typeContributions, JsonNodeStringType.INSTANCE);
78             }
79         } else if(dialect instanceof SQLServer2005Dialect) {
80             /* JSON */
81             if (enableJson) {
82                 this.contributeType(typeContributions, JsonStringType.INSTANCE);
83             }
84         } else if(dialect instanceof Oracle8iDialect) {
85             /* Date/Time */
86             this
87             .contributeType(typeContributions, OracleIntervalDayToSecondType.INSTANCE);
88             /* JSON */
89             if (enableJson) {
90                 this.contributeType(typeContributions, JsonStringType.INSTANCE)
91                 .contributeType(typeContributions, JsonBlobType.INSTANCE);
92             }
93         }
94
95         /* Basic */
96         this.contributeType(typeContributions, NullableCharacterType.INSTANCE)
97         /* Date/Time */
98         .contributeType(typeContributions, Iso8601MonthType.INSTANCE)
99         .contributeType(typeContributions, MonthDayDateType.INSTANCE)
100         .contributeType(typeContributions, MonthDayIntegerType.INSTANCE)
101         .contributeType(typeContributions, YearMonthDateType.INSTANCE)
102         .contributeType(typeContributions, YearMonthEpochType.INSTANCE)
103         .contributeType(typeContributions, YearMonthIntegerType.INSTANCE)
104         .contributeType(typeContributions, YearMonthTimestampType.INSTANCE);
105         /* JSON */
106         if (enableJson) {
107             this.contributeType(typeContributions, JsonType.INSTANCE);
108         }
109         /* Money and Currency API */
110         if(ReflectionUtils.getClassOrNull("org.javamoney.moneta.Money") != null) {
111             this.contributeType(typeContributions, CurrencyUnitType.INSTANCE)
112                 .contributeType(typeContributions, MonetaryAmountType.INSTANCE);
113         }
114     }
115
116     private HibernateTypesContributor contributeType(TypeContributions typeContributions, Object type) {
117         if (type instanceof BasicType) {
118             typeContributions.contributeType((BasicType) type);
119         } else if (type instanceof UserType) {
120             typeContributions.contributeType((UserType) type, type.getClass().getSimpleName());
121         } else if (type instanceof CompositeUserType) {
122             typeContributions.contributeType((CompositeUserType) type, type.getClass().getSimpleName());
123         } else {
124             throw new UnsupportedOperationException(
125                 String.format("The [%s] is not supported!", type.getClass())
126             );
127         }
128         return this;
129     }
130 }
131