将别名字段的类型信息保留在jOOQ中 [英] Keep type information of aliased fields in jOOQ

查看:98
本文介绍了将别名字段的类型信息保留在jOOQ中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jOOQ 进行联接查询,在该联接查询中,我必须将两个表中的列都别名为保持列名唯一.

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.

有没有一种方法可以避免在对列进行别名时发生的信息丢失?还是一种更好的方法,可以按照jOOQ的样式实现列名冲突的目标?

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"

推荐答案

由于仅从一个表t1j中进行选择,因此您可能能够从该表派生查询结果的行类型<R extends Record>.

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();

请注意,您似乎正在使用jOOQ 2.x,因此行类型尚不可用.

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

这篇关于将别名字段的类型信息保留在jOOQ中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆