-
Notifications
You must be signed in to change notification settings - Fork 362
/
JsonStringType.java
97 lines (83 loc) · 3.6 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
package com.vladmihalcea.hibernate.type.json;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vladmihalcea.hibernate.type.AbstractHibernateType;
import com.vladmihalcea.hibernate.type.json.internal.JsonStringSqlTypeDescriptor;
import com.vladmihalcea.hibernate.type.json.internal.JsonTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;
import org.hibernate.usertype.DynamicParameterizedType;
import java.lang.reflect.Type;
import java.util.Properties;
/**
* 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>
* If you are using <strong>Oracle</strong>, you should use this {@link JsonStringType} to map a <strong>{@code VARCHAR2}</strong> column type storing JSON.
* <p>
* If you are using <strong>SQL Server</strong>, you should use this {@link JsonStringType} to map an <strong>{@code NVARCHAR}</strong> column type storing JSON.
* <p>
* If you are using <strong>MySQL</strong>, you should use this {@link JsonStringType} to map the <strong>{@code json}</strong> column type.
* <p>
* If you are using <strong>PostgreSQL</strong>, then you should <strong>NOT</strong> use this {@link JsonStringType}. You should use {@link JsonBinaryType} instead.
* <p>
* For more details about how to use it, 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>.
*
* @author Vlad Mihalcea
*/
public class JsonStringType extends AbstractHibernateType<Object> implements DynamicParameterizedType {
public static final JsonStringType INSTANCE = new JsonStringType();
public JsonStringType() {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper())
);
}
public JsonStringType(Type javaType) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}
public JsonStringType(Configuration configuration) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(configuration.getObjectMapperWrapper()),
configuration
);
}
public JsonStringType(ObjectMapper objectMapper) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(new ObjectMapperWrapper(objectMapper))
);
}
public JsonStringType(ObjectMapperWrapper objectMapperWrapper) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(objectMapperWrapper)
);
}
public JsonStringType(ObjectMapper objectMapper, Type javaType) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(new ObjectMapperWrapper(objectMapper), javaType)
);
}
public JsonStringType(ObjectMapperWrapper objectMapperWrapper, Type javaType) {
super(
JsonStringSqlTypeDescriptor.INSTANCE,
new JsonTypeDescriptor(objectMapperWrapper, javaType)
);
}
@Override
public String getName() {
return "json";
}
@Override
protected boolean registerUnderJavaType() {
return true;
}
@Override
public void setParameterValues(Properties parameters) {
((JsonTypeDescriptor) getJavaTypeDescriptor()).setParameterValues(parameters);
}
}