Class JsonType

  • All Implemented Interfaces:
    java.io.Serializable, org.hibernate.type.BasicType, org.hibernate.type.ProcedureParameterExtractionAware<java.lang.Object>, org.hibernate.type.ProcedureParameterNamedBinder, org.hibernate.type.SingleColumnType<java.lang.Object>, org.hibernate.type.StringRepresentableType<java.lang.Object>, org.hibernate.type.Type, org.hibernate.usertype.DynamicParameterizedType, org.hibernate.usertype.ParameterizedType

    public class JsonType
    extends AbstractHibernateType<java.lang.Object>
    implements org.hibernate.usertype.DynamicParameterizedType

    JsonType allows you to map any given JSON object (e.g., POJO, Map<String, Object>, List<T>, JsonNode) on any of the following database systems:

    • PostgreSQL - for both jsonb and json column types
    • MySQL - for the json column type
    • SQL Server - for the NVARCHAR column type storing JSON
    • Oracle - for the JSON column type if you're using Oracle 21c or the VARCHAR column type storing JSON if you're using an older Oracle version
    • H2 - for the json column type

    If you switch to Oracle 21c from an older version, then you should also migrate your JSON columns to the native JSON type since this binary type performs better than VARCHAR2 or BLOB column types.

    However, if you don't want to migrate to the new JSON data type, then you just have to provide the column type via the JPA Column.columnDefinition() attribute, like in the following example:

     @Type(type = "com.vladmihalcea.hibernate.type.json.JsonType")
     @Column(columnDefinition = "VARCHAR2")
     

    For more details about how to use the JsonType, check out this article on vladmihalcea.com.

    If you are using Oracle and want to store JSON objects in a BLOB column type, then you can use the JsonBlobType instead. For more details, check out this article on vladmihalcea.com.

    Or, you can use the JsonType, but you'll have to specify the underlying column type using the JPA Column.columnDefinition() attribute, like this:

     @Type(type = "com.vladmihalcea.hibernate.type.json.JsonType")
     @Column(columnDefinition = "BLOB")
     
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.hibernate.usertype.DynamicParameterizedType

        org.hibernate.usertype.DynamicParameterizedType.ParameterType
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static JsonType INSTANCE  
      • Fields inherited from interface org.hibernate.usertype.DynamicParameterizedType

        ACCESS_TYPE, ENTITY, IS_DYNAMIC, IS_PRIMARY_KEY, PARAMETER_TYPE, PROPERTY, RETURNED_CLASS, XPROPERTY
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.String getName()  
      void setParameterValues​(java.util.Properties parameters)  
      • Methods inherited from class org.hibernate.type.AbstractSingleColumnStandardBasicType

        nullSafeSet, sqlType
      • Methods inherited from class org.hibernate.type.AbstractStandardBasicType

        assemble, beforeAssemble, canDoExtraction, canDoSetting, compare, deepCopy, deepCopy, defaultSizes, dictatedSizes, disassemble, extract, extract, fromString, fromStringValue, get, getColumnSpan, getDefaultSize, getDictatedSize, getHashCode, getHashCode, getJavaTypeDescriptor, getMutabilityPlan, getRegistrationKeys, getReplacement, getReturnedClass, getSemiResolvedType, getSqlTypeDescriptor, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isDirty, isDirty, isEntityType, isEqual, isEqual, isModified, isMutable, isSame, isXMLElement, nullSafeGet, nullSafeGet, nullSafeGet, nullSafeGet, nullSafeSet, nullSafeSet, nullSafeSet, nullSafeSet, registerUnderJavaType, remapSqlTypeDescriptor, replace, replace, resolve, semiResolve, set, setJavaTypeDescriptor, setSqlTypeDescriptor, sqlTypes, toColumnNullness, toLoggableString, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface org.hibernate.type.SingleColumnType

        fromStringValue, get, nullSafeGet, set, toString
      • Methods inherited from interface org.hibernate.type.Type

        assemble, beforeAssemble, compare, deepCopy, defaultSizes, dictatedSizes, disassemble, getColumnSpan, getHashCode, getHashCode, getReturnedClass, getSemiResolvedType, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isDirty, isEntityType, isEqual, isEqual, isModified, isMutable, isSame, nullSafeGet, nullSafeGet, nullSafeSet, replace, replace, resolve, resolve, semiResolve, sqlTypes, toColumnNullness, toLoggableString
    • Field Detail

      • INSTANCE

        public static final JsonType INSTANCE
    • Constructor Detail

      • JsonType

        public JsonType()
      • JsonType

        public JsonType​(java.lang.reflect.Type javaType)
      • JsonType

        public JsonType​(com.fasterxml.jackson.databind.ObjectMapper objectMapper)
      • JsonType

        public JsonType​(com.fasterxml.jackson.databind.ObjectMapper objectMapper,
                        java.lang.reflect.Type javaType)
      • JsonType

        public JsonType​(ObjectMapperWrapper objectMapperWrapper,
                        java.lang.reflect.Type javaType)
    • Method Detail

      • getName

        public java.lang.String getName()
        Specified by:
        getName in interface org.hibernate.type.Type
      • setParameterValues

        public void setParameterValues​(java.util.Properties parameters)
        Specified by:
        setParameterValues in interface org.hibernate.usertype.ParameterizedType