/
JsonBlobType.java
96 lines (83 loc) · 3.54 KB
/
JsonBlobType.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
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.util.Configuration;
import com.vladmihalcea.hibernate.type.util.ObjectMapperWrapper;
import org.hibernate.type.descriptor.jdbc.BlobJdbcType;
import java.lang.reflect.Type;
import java.sql.Blob;
/**
* <p>
* Maps any given Java object on a JSON column type that is managed via {@link java.sql.PreparedStatement#setBlob(int, Blob)} at JDBC Driver level.
* </p>
* <p>
* If you are using <strong>Oracle</strong>, you can use this {@link JsonBlobType} to map a {@code BLOB} column type storing JSON.
* </p>
* <p>
* For more details about how to use it, check out <a href="https://vladmihalcea.com/oracle-json-jpa-hibernate/">this article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
* </p>
* <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 JsonBlobType extends MutableDynamicParameterizedType<Object, BlobJdbcType, JsonJavaTypeDescriptor> {
public static final JsonBlobType INSTANCE = new JsonBlobType();
public JsonBlobType() {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper())
);
}
public JsonBlobType(Type javaType) {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(Configuration.INSTANCE.getObjectMapperWrapper(), javaType)
);
}
public JsonBlobType(Configuration configuration) {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(configuration.getObjectMapperWrapper())
);
}
public JsonBlobType(org.hibernate.type.spi.TypeBootstrapContext typeBootstrapContext) {
this(new Configuration(typeBootstrapContext.getConfigurationSettings()));
}
public JsonBlobType(ObjectMapper objectMapper) {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(new ObjectMapperWrapper(objectMapper))
);
}
public JsonBlobType(ObjectMapperWrapper objectMapperWrapper) {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(objectMapperWrapper)
);
}
public JsonBlobType(ObjectMapper objectMapper, Type javaType) {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(new ObjectMapperWrapper(objectMapper), javaType)
);
}
public JsonBlobType(ObjectMapperWrapper objectMapperWrapper, Type javaType) {
super(
Object.class,
org.hibernate.type.descriptor.jdbc.BlobJdbcType.DEFAULT,
new JsonJavaTypeDescriptor(objectMapperWrapper, javaType)
);
}
public String getName() {
return "jsonb-lob";
}
}