All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vladmihalcea.hibernate.type.json.JsonType Maven / Gradle / Ivy

There is a newer version: 2.21.1
Show newest version
package com.vladmihalcea.hibernate.type.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vladmihalcea.hibernate.type.MutableDynamicParameterizedType;
import com.vladmihalcea.hibernate.type.json.internal.JsonJavaTypeDescriptor;
import com.vladmihalcea.hibernate.type.json.internal.JsonJdbcTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;

import java.lang.reflect.Type;

/**
 * 

* {@link 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 {@code JSON} columns to the native JSON type since this binary type performs better than * {@code VARCHAR2} or {@code BLOB} column types. *

*

* However, if you don't want to migrate to the new {@code JSON} data type, * then you just have to provide the column type via the JPA {@link jakarta.persistence.Column#columnDefinition()} attribute, * like in the following example: *

*
 * {@code @Type(}JsonType.class)
 * {@code @Column(}columnDefinition = "VARCHAR2")
 * 
*

* For more details about how to use the {@link 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 {@link JsonBlobType} instead. For more details, check out this article on vladmihalcea.com. *

*

* Or, you can use the {@link JsonType}, but you'll have to specify the underlying column type * using the JPA {@link jakarta.persistence.Column#columnDefinition()} attribute, like this: *

*
 * {@code @Type(}JsonType.class)
 * {@code @Column(}columnDefinition = "BLOB")
 * 
* @author Vlad Mihalcea */ public class JsonType extends MutableDynamicParameterizedType { public static final JsonType INSTANCE = new JsonType(); public JsonType() { super( Object.class, new JsonJdbcTypeDescriptor(), new JsonJavaTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper()) ); } public JsonType(Type javaType) { super( Object.class, new JsonJdbcTypeDescriptor(), new JsonJavaTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType) ); } public JsonType(Configuration configuration) { super( Object.class, new JsonJdbcTypeDescriptor(configuration.getProperties()), new JsonJavaTypeDescriptor(configuration.getObjectMapperWrapper()), configuration ); } public JsonType(org.hibernate.type.spi.TypeBootstrapContext typeBootstrapContext) { this(new Configuration(typeBootstrapContext.getConfigurationSettings())); } public JsonType(ObjectMapper objectMapper) { super( Object.class, new JsonJdbcTypeDescriptor(), new JsonJavaTypeDescriptor(new ObjectMapperWrapper(objectMapper)) ); } public JsonType(ObjectMapperWrapper objectMapperWrapper) { super( Object.class, new JsonJdbcTypeDescriptor(), new JsonJavaTypeDescriptor(objectMapperWrapper) ); } public JsonType(ObjectMapper objectMapper, Type javaType) { super( Object.class, new JsonJdbcTypeDescriptor(), new JsonJavaTypeDescriptor(new ObjectMapperWrapper(objectMapper), javaType) ); } public JsonType(ObjectMapperWrapper objectMapperWrapper, Type javaType) { super( Object.class, new JsonJdbcTypeDescriptor(), new JsonJavaTypeDescriptor(objectMapperWrapper, javaType) ); } public String getName() { return "json"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy