-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathdbjson.html
More file actions
332 lines (313 loc) · 12.8 KB
/
dbjson.html
File metadata and controls
332 lines (313 loc) · 12.8 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<!doctype html>
<html lang="en">
<head>
<title>Mapping | Ebean</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="shortcut icon" href="/images/favicon.ico">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto|Source+Sans+Pro|Ubuntu&display=swap">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.0/css/all.css" integrity="sha384-lKuwvrZot6UHsBSfcMvOkWwlCMgc0TaWr+30HWe3a4ltaBwTZhyTEggF5tJv8tbt" crossorigin="anonymous">
<link rel="stylesheet" href="/css/reset3.css">
<link rel="stylesheet" href="/css/site3.css">
<link rel="stylesheet" href="/css/pygments3.css">
</head>
<body>
<div id="main">
<div id="banner">
<header>
<nav id="top">
<h1 id="breadcrumb">
<a class="nav-logo" href="/"><img src="/images/logo-200.png" height="35"></a> <a href="/docs">Documentation</a><span class="sep"> / </span><a href="/docs/mapping/">Mapping</a><span class="sep"> / </span><a href="/docs/mapping/extension">Extension</a><span class="sep"> / </span><span class="last">@DbJson</span>
</h1>
<ul>
<li><a onclick="toggleTheme();" title="switch dark light theme"><i class="fas fa-adjust"></i></a></li>
</ul>
</nav>
</header>
</div>
<div class="grid grid-docs">
<aside>
<nav class="side">
<ul>
<li class="nav0 ">
<a href="/docs/getting-started">Getting started</a>
</li>
<li class="nav0 ">
<a href="/docs/intro">Introduction</a>
</li>
<li class="nav0 active">
<a class="active" href="/docs">Documentation</a>
<ul>
<li class="nav1 ">
<a href="/docs/agents">AI Agents</a>
</li>
<li class="nav1 ">
<a href="/docs/best-practice">Best practice</a>
</li>
<li class="nav1 ">
<a href="/docs/query">Query</a>
</li>
<li class="nav1 ">
<a href="/docs/persist">Persist</a>
</li>
<li class="nav1 ">
<a href="/docs/transactions">Transactions</a>
</li>
<li class="nav1 active">
<a class="active" href="/docs/mapping">Mapping</a>
<ul class="nav">
<li class="nav1 ">
<a href="/docs/mapping/type">Types</a>
</li>
<li class="nav1 ">
<a href="/docs/mapping/jpa">JPA</a>
</li>
<li class="nav1 active">
<a class="active" href="/docs/mapping/extensions">Extensions</a>
<ul class="nav">
<li >
<a href="/docs/mapping/extensions/identity">@Identity</a>
</li>
<li >
<a href="/docs/mapping/extensions/dbindex">@Index</a>
</li>
<li >
<a href="/docs/mapping/extensions/dbenumvalue">@DbEnumValue</a>
</li>
<li class="active">
<a class="active" href="/docs/mapping/extensions/dbjson">@DbJson</a>
<ul class="nav">
<li><a href="#overview">Overview</a></li>
<li><a href="#mixed">Mixing structured and unstructured</a></li>
<li><a href="#simple-types">Mapping simple types</a></li>
<li><a href="#jackson">Mapping with Jackson ObjectMapper</a></li>
<li><a href="#fallback">Fallback mapping</a></li>
<li><a href="#expressions">Query expressions</a></li>
</ul>
</li>
<li >
<a href="/docs/mapping/extensions/dbarray">@DbArray</a>
</li>
<li >
<a href="/docs/mapping/extensions/dbmap">@DbMap</a>
</li>
<li >
<a href="/docs/mapping/extensions/dbforeignkey">@DbForeignKey</a>
</li>
<li >
<a href="/docs/mapping/extensions/dbcomment">@DbComment</a>
</li>
<li >
<a href="/docs/mapping/extensions/dbpartition">@DbPartition</a>
</li>
<li >
<a href="/docs/mapping/extensions/when-created">@WhenCreated</a>
</li>
<li >
<a href="/docs/mapping/extensions/when-modified">@WhenModified</a>
</li>
<li >
<a href="/docs/mapping/extensions/who-created">@WhoCreated</a>
</li>
<li >
<a href="/docs/mapping/extensions/who-modified">@WhoModified</a>
</li>
<li >
<a href="/docs/mapping/extensions/soft-delete">@SoftDelete</a>
</li>
<li >
<a href="/docs/mapping/extensions/encrypted">@Encrypted</a>
</li>
<li >
<a href="/docs/mapping/extensions/formula">@Formula</a>
</li>
<li >
<a href="/docs/mapping/extensions/view">@View</a>
</li>
<li >
<a href="/docs/mapping/extensions/history">@History</a>
</li>
<li >
<a href="/docs/mapping/extensions/change-log">@ChangeLog</a>
</li>
<li >
<a href="/docs/mapping/extensions/read-audit">@ReadAudit</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="nav1 ">
<a href="/docs/ddl-generation">DDL & Migrations</a>
</li>
<li class="nav1 ">
<a href="/docs/logging">Logging</a>
</li>
<li class="nav1 ">
<a href="/docs/testing">Testing</a>
</li>
<li class="nav1 ">
<a href="/docs/read-replicas">Read Replicas</a>
</li>
<li class="nav1 ">
<a href="/docs/database">Database platforms</a>
</li>
<li class="nav1 ">
<a href="/docs/multi-database">Multiple databases</a>
</li>
<li class="nav1 ">
<a href="/docs/kotlin">Kotlin</a>
</li>
<li><a href="/docs/tuning">Tuning</a></li>
<li class="nav1 ">
<a href="/docs/features">Features</a>
</li>
</ul>
</li>
<li class="nav0 ">
<a href="/support">Getting help</a>
</li>
<li class="nav0 ">
<a target="_blank" href="/apidoc/13">API Javadoc</a>
</li>
<li class="nav0 ">
<a href="/videos">Videos</a>
</li>
<li class="nav0 ">
<a href="/docs/upgrading">Upgrading</a>
</li>
<li class="nav0 ">
<a href="/docs/deprecated">Deprecated</a>
</li>
<li class="nav0 ">
<a href="/releases">Releases</a>
</li>
</ul>
</nav>
</aside>
<article>
<form action="https://www.google.com/search" method="get" class="inline-form">
<input type="hidden" name="as_sitesearch" value="ebean.io">
<div id="page-search">
<div class="input-group">
<input class="frm" name="q" id="searchinput" type="text" placeholder="Search... (press 's' to focus)" data-placeholder-focus="Search... (use '↑', '↓' and '⏎' to select results)" data-placeholder-blur="Search... (press 's' to focus)" autocomplete="off">
<div class="input-group-btn">
<button class="frm" type="submit"><i class="fas fa-search"></i></button>
</div>
</div>
<div id="page-search-results" style="display: none;">
<ul id="search-results-container" class="search-results"><li class=" active"><a href="/docs" title="Docs"><span style="color:#777;">Docs</span> Documentation </a></li><li class=""><small style="color:#999;">And 101 more...</small></li></ul>
</div>
</div>
</form>
<h2 id="overview">Overview</h2>
<p>
Ebean provides <code>@DbJson</code> and <code>@DbJsonB</code> for mapping properties as
JSON documents into Postgres JSON, Postgres JSONB, CLOB, VARCHAR or BLOB.
</p>
<p>
Both <code>Postgres</code> and <code>Oracle</code> (and shortly <code>MySql</code>) provide
functions/expressions for querying and manipulating the JSON.
</p>
<p>
Ebean also has <code>ElasticSearch</code> integration so even without the built in database
support we can use that integration to effectively replicate to ElasticSearch and use it
to search our data.
</p>
<h2 id="mixed">Mixing structured and unstructured</h2>
<p>
What these capabilities do is enable designers/developers to mix unstructured (or semi-structured)
document content with our typically highly structured data.
</p>
<p>
An example use case might be storing 'user preferences' where the data naturally comes in as JSON,
has potentially deep nesting and where we only need limited queries/expressions on the document attributes.
In this type of case the use of unstructured document storage as JSON can be a good fit.
</p>
<h2 id="simple-types">Mapping simple types</h2>
<p>
If the mapping is to <code>String</code>, <code>Long</code> or <code>Map<String,Object></code> types
then Ebean will use it's built in JSON support to handle the marshalling to/from JSON.
</p>
<div class="syntax java"><div class="highlight"><pre><span></span><span class="nd">@DbJsonB</span>
<span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">></span> <span class="n">content</span><span class="o">;</span>
<span class="nd">@DbJsonB</span>
<span class="n">List</span><span class="o"><</span><span class="n">String</span><span class="o">></span> <span class="n">contents</span><span class="o">;</span>
</pre></div>
</div>
<p>
With these types note that Ebean will use <code>ModifyAware</code> collections so that it knows if the
property is <code>dirty</code> and needs to be included in an update. If the type is not ModifyAware then
it must be treated as dirty and included in an update.
</p>
<h2 id="jackson">Mapping with Jackson ObjectMapper</h2>
<p>
If the type mapped is not one of <code>String</code>, <code>Long</code> or <code>Map<String,Object></code> types
then Ebean will use Jackson ObjectMapper to handle the marshalling to/from JSON.
In this case <code>jackson-databind</code> becomes a required dependency.
</p>
<div class="syntax java"><div class="highlight"><pre><span></span><span class="c1">// Jackson ObjectMapper used here</span>
<span class="nd">@DbJsonB</span>
<span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span><span class="n">SomePojo</span><span class="o">></span> <span class="n">content</span><span class="o">;</span>
<span class="c1">// Jackson ObjectMapper used here</span>
<span class="nd">@DbJsonB</span>
<span class="n">SomePojo</span> <span class="n">content</span><span class="o">;</span>
</pre></div>
</div>
<p>
Note that when we use Jackson for marshalling the property type is not <code>ModifyAware</code> and that
means that it must be treated as <code>dirty</code> and included in updates. It will only be included in
updates if the property is actually loaded of course and so if you know a use case will not update the content
it can be worth while to not load it in the first place (with the additional benefit that it won't be automatically
included in the update).
</p>
<h4>DatabaseBuilder</h4>
<p>
An ObjectMapper instance can be configured on the <code>DatabaseBuilder</code> via <code>objectMapper()</code>.
If an ObjectMapper is not specified in this way an instance will be created for use by Ebean
(if Jackson ObjectMapper is detected in the classpath).
</p>
<h2 id="fallback">Fallback mapping</h2>
<p>
When Postgres is not used then fallback is to store JSON into VARCHAR, CLOB or BLOB database types.
If the <code>length</code> attribute is specified like <code>@DbJson(length=1000)</code>
or <code>@DbJsonB(length=1000)</code> then VARCHAR is the fallback type and the length defined is the
length of the varchar column definition.
</p>
<div class="syntax java"><div class="highlight"><pre><span></span><span class="c1">// fallback to store in VARCHAR(800)</span>
<span class="nd">@DbJson</span><span class="o">(</span><span class="nx">length</span><span class="o">=</span><span class="mi">800</span><span class="o">)</span>
<span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">></span> <span class="n">content</span><span class="o">;</span>
</pre></div>
</div>
<p>
The storage can be explicitly defined using the <code>storage</code> attribute.
</p>
<div class="syntax java"><div class="highlight"><pre><span></span><span class="c1">// explicitly store using BLOB</span>
<span class="nd">@DbJson</span><span class="o">(</span><span class="nx">storage</span> <span class="o">=</span> <span class="nx">DbJsonType</span><span class="o">.</span><span class="na">BLOB</span><span class="o">)</span>
<span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span><span class="n">Object</span><span class="o">></span> <span class="n">content</span><span class="o">;</span>
</pre></div>
</div>
<p>
The default fallback for JSON and JSONB types is CLOB and this is used when both the length and
storage attributes are not defined.
</p>
<h2 id="expressions">Query expressions</h2>
<p>
Refer to <a href="/docs/features/json-in-db">json-in-db</a> documentation for details on the query expressions
and how those translate for both Postgres and Oracle expressions.
</p>
</article>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="/js/site3.js"></script>
<script src="/js/search3.js"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-75181644-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-75181644-1');
</script>
</body>
</html>