Keep type information of aliased fields in jOOQ

帅比萌擦擦* 提交于 2019-12-11 13:21:45

问题


I'm doing a join query with jOOQ, in which I have to alias the columns from both tables to keep the column-names unique.

Is there a way to circumvent the information loss that occurs when I do aliasing to columns? Or a better way to achieve the goal of columns name clashes following jOOQ's style?

When I do alias all the Fields, all type information is lost:

List<Field<?>> columns = factory.select().from(t1j).limit(0).fetch().getFields();
List<Field<?>> aliases = new LinkedList<Field<?>>();
for (Field f : columns) {
    Field alias = Factory.fieldByName(t1j.getName(), f.getName())
                         .as(f.getName() + "_t1");
    aliases.add(alias);
}

// columns.get(0).getType() == "class java.lang.String"
// aliases.get(0).getType() == "class java.lang.Object"

回答1:


Since you're selecting only from one table t1j, you might be able to derive the row type <R extends Record> of your query result from that table.

Row types can be explicitly constructed using the various DSL.row(...) methods that were introduced in jOOQ 3.0:

Row2<Integer, String> row = DSL.row(INT_FIELD, STRING_FIELD);

You can then use that row type to express a more typesafe select statement:

Result<Record2<Integer, String>> result =
DSL.using(configuration)
   .select(row.field1().as("f1"), row.field2().as("f2"))
   .from(t1j)
   .limit(0)
   .fetch();

Note that you seem to be using jOOQ 2.x, so row types are not yet available to you.



来源:https://stackoverflow.com/questions/19638337/keep-type-information-of-aliased-fields-in-jooq

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!