/
JsonType.java
100 lines (87 loc) · 3.75 KB
/
JsonType.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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;
/**
* <p>
* {@link JsonType} allows you to map any given JSON object (e.g., POJO, <code>Map<String, Object></code>, List<T>, <code>JsonNode</code>) on any of the following database systems:
* </p>
* <ul>
* <li><strong>PostgreSQL</strong> - for both <code>jsonb</code> and <code>json</code> column types</li>
* <li><strong>MySQL</strong> - for the <code>json</code> column type</li>
* <li><strong>SQL Server</strong> - for the <code>NVARCHAR</code> column type storing JSON</li>
* <li><strong>Oracle</strong> - for the <code>VARCHAR</code> column type storing JSON</li>
* <li><strong>H2</strong> - for the <code>json</code> column type</li>
* </ul>
*
* <p>
* For more details about how to use the {@link JsonType}, check out <a href="https://vladmihalcea.com/how-to-map-json-objects-using-generic-hibernate-types/">this article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
* </p>
* <p>
* If you are using <strong>Oracle</strong> and want to store JSON objects in a <code>BLOB</code> column types, then you should use the {@link JsonBlobType} instead. For more details, check out <a href="https://vladmihalcea.com/oracle-json-jpa-hibernate/">this article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
* </p>
*
* @author Vlad Mihalcea
*/
public class JsonType extends MutableDynamicParameterizedType<Object, JsonJdbcTypeDescriptor, JsonJavaTypeDescriptor> {
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(),
new JsonJavaTypeDescriptor(configuration.getObjectMapperWrapper())
);
}
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";
}
}