/
JsonStringType.java
102 lines (89 loc) · 4.51 KB
/
JsonStringType.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
101
102
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.JsonStringJdbcTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;
import java.lang.reflect.Type;
/**
* <p>
* Maps any given Java object on a JSON column type that is managed via {@link java.sql.PreparedStatement#setString(int, String)} at JDBC Driver level.
* </p>
* <ul>
* <li>If you are using <strong>Oracle</strong>, you can use this {@link JsonStringType} to map a <strong>{@code VARCHAR2}</strong> column type storing JSON. 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>.
* </li>
* <li>
* If you are using <strong>SQL Server</strong>, you can use this {@link JsonStringType} to map an <strong>{@code NVARCHAR}</strong> column type storing JSON. For more details, check out <a href="https://vladmihalcea.com/sql-server-json-hibernate/">this article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
* </li>
* <li>
* If you are using <strong>MySQL</strong>, you can use this {@link JsonStringType} to map the <strong>{@code json}</strong> column type. For more details, 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>.
* </li>
* <li>
* If you are using <strong>PostgreSQL</strong>, then you should <strong>NOT</strong> use this {@link JsonStringType}. You should use {@link JsonBinaryType} instead. For more details, 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>.
* </li>
* </ul>
* <p>
* If you want to use a more portable Hibernate <code>Type</code> that can work on <strong>Oracle</strong>, <strong>SQL Server</strong>, <strong>PostgreSQL</strong>, <strong>MySQL</strong>, or <strong>H2</strong> without any configuration changes, then you should use the {@link JsonType} instead.
* </p>
*
* @author Vlad Mihalcea
*/
public class JsonStringType extends MutableDynamicParameterizedType<Object, JsonStringJdbcTypeDescriptor, JsonJavaTypeDescriptor> {
public static final JsonStringType INSTANCE = new JsonStringType();
public JsonStringType() {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper())
);
}
public JsonStringType(Type javaType) {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}
public JsonStringType(Configuration configuration) {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(configuration.getObjectMapperWrapper())
);
}
public JsonStringType(org.hibernate.type.spi.TypeBootstrapContext typeBootstrapContext) {
this(new Configuration(typeBootstrapContext.getConfigurationSettings()));
}
public JsonStringType(ObjectMapper objectMapper) {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(new ObjectMapperWrapper(objectMapper))
);
}
public JsonStringType(ObjectMapperWrapper objectMapperWrapper) {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(objectMapperWrapper)
);
}
public JsonStringType(ObjectMapper objectMapper, Type javaType) {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(new ObjectMapperWrapper(objectMapper), javaType)
);
}
public JsonStringType(ObjectMapperWrapper objectMapperWrapper, Type javaType) {
super(
Object.class,
JsonStringJdbcTypeDescriptor.INSTANCE,
new JsonJavaTypeDescriptor(objectMapperWrapper, javaType)
);
}
public String getName() {
return "json";
}
}