问题
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