You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// ... using startRow and work with the row, see how the order doesn't matter:
304
-
writer.startRow((row) -> {
305
-
row.setText("value_text", "Hi");
306
-
row.setInteger("value_int", 1);
307
-
});
308
-
309
-
}
310
-
}
311
-
312
-
// Now assert, that we have written 10000 entities:
313
-
314
-
Assert.assertEquals(10000, getRowCount());
315
-
}
316
-
}
317
-
```
318
-
319
-
If you need to customize the Null Character Handling, then you can use the ``setNullCharacterHandler(Function<String, String> nullCharacterHandler)`` function.
320
-
321
-
## Using the AbstractMapping ##
322
-
323
-
The ``AbstractMapping`` is the second possible way to map a POJO for usage in PgBulkInsert. Imagine we want to bulk insert a large amount of people
324
-
into a PostgreSQL database. Each ``Person`` has a first name, a last name and a birthdate.
325
-
326
-
### Database Table ###
327
-
328
-
The table in the PostgreSQL database might look like this:
329
-
330
-
```sql
331
-
CREATETABLEsample.unit_test
332
-
(
333
-
first_name text,
334
-
last_name text,
335
-
birth_date date
336
-
);
337
-
```
338
-
339
-
### Domain Model ###
340
-
341
-
The domain model in the application might look like this:
342
-
343
-
```java
344
-
privateclassPerson {
345
-
346
-
privateString firstName;
347
-
348
-
privateString lastName;
349
-
350
-
privateLocalDate birthDate;
351
-
352
-
publicPerson() {}
353
-
354
-
publicStringgetFirstName() {
355
-
return firstName;
356
-
}
357
-
358
-
publicvoidsetFirstName(StringfirstName) {
359
-
this.firstName = firstName;
360
-
}
361
-
362
-
publicStringgetLastName() {
363
-
return lastName;
364
-
}
365
-
366
-
publicvoidsetLastName(StringlastName) {
367
-
this.lastName = lastName;
368
-
}
369
-
370
-
publicLocalDategetBirthDate() {
371
-
return birthDate;
372
-
}
373
-
374
-
publicvoidsetBirthDate(LocalDatebirthDate) {
375
-
this.birthDate = birthDate;
376
-
}
377
-
378
-
}
379
-
```
380
-
381
-
### Bulk Inserter ###
382
-
383
-
Then you have to implement the ``AbstractMapping<Person>``, which defines the mapping between the table and the domain model.
And finally we can write a Unit Test to insert ``100000`` people into the database. You can find the entire Unit Test on GitHub as [IntegrationTest.java].
So for every data type that also has a primitive type, you can add a "Primitive" suffix to the method name like:
450
-
451
-
* ```mapBooleanPrimitive``
452
-
453
-
This will use the primitive type and prevent boxing and unboxing of values.
454
-
455
-
### How can I write a ``java.sql.Timestamp``? ###
456
-
457
-
You probably have Java classes with a ``java.sql.Timestamp`` in your application. Now if you use the ``AbstractMapping`` or a ``SimpleRowWriter`` it expects a ``LocalDateTime``. Here is how to map a ``java.sql.Timestamp``.
458
-
459
-
Imagine you have an ``EMail`` class with a property ``emailCreateTime``, that is using a ``java.sql.Timestamp`` to
460
-
represent the time. The column name in Postgres is ``email_create_time`` and you are using a ``timestamp`` data type.
461
-
462
-
To map the ``java.sql.Timestamp`` you would write the ``mapTimeStamp`` method like this:
463
-
464
-
```java
465
-
mapTimeStamp("email_create_time", x -> x.getEmailCreateTime() !=null? x.getEmailCreateTime().toLocalDateTime() :null);
If you see the error message ``invalid byte sequence for encoding "UTF8": 0x00`` your data contains Null Characters. Although ``0x00`` is totally valid UTF-8... PostgreSQL does not support writing it, because it uses C-style string termination internally.
493
-
494
-
PgBulkInsert allows you to enable a Null Value handling, that removes all ``0x00`` occurences and replaces them with an empty string:
*[Postgres on the wire - A look at the PostgreSQL wire protocol (PGCon 2014)](https://www.pgcon.org/2014/schedule/attachments/330_postgres-for-the-wire.pdf)
0 commit comments