Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jOOQ mapper from POJO to Record

Tags:

java

jooq

Using jOOQ 3.8.6, I had to implement record mapper to convert from Record to Pojo because of some UDT in the fetched records. Now, I wonder how to do the opposite when creating a Record from a Pojo.

public void update(MyTable pojo){ 
  MyTableRecord record = dsl.newRecord(tables.MyTable.MY_TABLE, pojo);
  record.store();
}

I have a

org.jooq.exception.MappingException: An error ocurred when mapping record from class tables.pojos.MyTable

because of a

Caused by: org.jooq.exception.DataTypeException: Cannot convert from MyType (class udt.pojos.MyType) to class udt.records.MyTypeRecord

I think I have to register a custom converter from POJO to Record. Does anyone know how?

like image 657
mat_boy Avatar asked Dec 05 '16 19:12

mat_boy


People also ask

How do you create a record in JOOQ?

// Create a new record BookRecord book1 = create. newRecord(BOOK); // Insert the record: INSERT INTO BOOK (TITLE) VALUES ('1984'); book1. setTitle("1984"); book1. store(); // Update the record: UPDATE BOOK SET PUBLISHED_IN = 1984 WHERE ID = [id] book1.

What is record in JOOQ?

A record essentially combines a list of columns ( Field ) with a corresponding list of values, each value being of the respective field's type.

What is pojo?

POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program.


1 Answers

You're looking for the RecordUnmapper feature (issue #2520), which has not been implemented yet as of jOOQ 3.8

You have at least these two possible workarounds:

  1. Don't use a RecordMapper, but a Converter or data type Binding instead. Those will allow you to implement the conversion in two ways, and it will not only apply to your POJOs, but also to your Records
  2. Do a manual "unmapping" step, prior to calling DSLContext.newRecord(Table, Object)
like image 129
Lukas Eder Avatar answered Sep 17 '22 10:09

Lukas Eder