java - jooq cannot convert from class a to class b -
i have problem, think can easly solved, can't it.
i have class , b , class stored in postgres db:
public class a() { private b b; ... } public class b() { private string firstname; private string lastname; private string personid; private string username; }
in database, object of class b stored string username in entity. have object of class db in app, have read record , create object of class b. want create query jooq, jooq not know how convert string username instance of class b. how tell jooq how should map database object object of class a.
a class equivalent person , b class equivalent executor.
my query
return jooq.select() .from(person) .where(person.id.eq(id)) .fetchoneinto(person.class);
this exception thrown query
caused by: org.jooq.exception.datatypeexception: cannot convert superadmin (class java.lang.string) class fi.ssm.oksa.domain.person.executor @ org.jooq.tools.convert$convertall.fail(convert.java:1118) ~[jooq-3.8.5.jar:na] ...
i think should use http://www.jooq.org/doc/3.8/manual/code-generation/custom-data-type-bindings/ can't implement it.
there different ways solve this:
use jooq's undocumented (as of version 3.8) nested record mapping feature
jooq has undocumented feature in defaultrecordmapper
allows nest result records when use of into(class)
methods. have manually specify "path" of nested record if using ordbms nested records, such:
jooq.select( person.first_name.as("executor.first_name"), person.last_name.as("executor.last_name"), person.person_id.as("executor.person_id"), person.user_name.as("executor.user_name")) .from(person) .where(person.id.eq(id)) .fetchoneinto(person.class);
when alias columns way, defaultrecordmapper
search executor
property in person
class, , put trailing sub-path nested object.
in example, i'm going assume modified versions of classes:
// instead of public class person { private executor executor; ... } // instead of b public class executor { private string firstname; private string lastname; private string personid; private string username; }
override defaultrecordmapper
you can implement own algorithms mapping when using into(class)
methods documented here:
http://www.jooq.org/doc/latest/manual/sql-execution/fetching/pojos-with-recordmapper-provider
use explicit record mapper:
of course, nothing forces rely on jooq's built-in automatic mapping features. write own algorithm this, instance:
jooq.select() .from(person) .where(person.id.eq(id)) .fetchone(r -> new person(new executor(r.get(person.first_name), ...));
Comments
Post a Comment