缩小jooq选择时避免强制转换 [英] Avoiding casts when narrowing jooq selects
问题描述
假设我有一个图书数据库,我想检查CLRS图书的作者是否正确。
Let's say I have a book database and I want to check whether the CLRS book has the correct authors.
假设 private static final String CLRS_title =算法简介;
@Test
public void CLRS_is_written_by_CLRS(){
//given
SelectConditionStep<Record> query = create
.select()
.from(
(
BOOK.leftOuterJoin(BOOK_AUTHOR).on(BOOK.ID.eq(BOOK_AUTHOR.BOOKID))
).leftOuterJoin(AUTHOR).on(AUTHOR.ID.eq(BOOK_AUTHOR.AUTHORID))
)
.where(BOOK.TITLE.eq(CLRS_title))
;
//when
Result<Record> result = query.fetch();
List<String> authorNames = result.stream().map(r-> r.getValue(AUTHOR.LASTNAME)).collect(Collectors.toList());
//then
assertThat(authorNames.size(),is(4));
assertThat(authorNames.containsAll(Arrays.asList("Cormen","Leiserson","Rivest","Stein")), is(true));
}
(请忽略连接整个表的效率很低当我们只对一本书感兴趣时,如有必要,我将对此做一个单独的问题。)
(Please ignore that it's very inefficient to join a whole table when we're just interested in one book, I'll make a separate question about that, if/when necessary.)
我现在只想选择 AUTHOR.LASTNAME
属性而不是所有内容。
I now just want to select the AUTHOR.LASTNAME
property instead of just everything.
SelectConditionStep<Record1<String>> query = create
.select(AUTHOR.LASTNAME.as("AuthorName"))
.from(
(
BOOK.leftOuterJoin(BOOK_AUTHOR).on(BOOK.ID.eq(BOOK_AUTHOR.BOOKID))
).leftOuterJoin(AUTHOR).on(AUTHOR.ID.eq(BOOK_AUTHOR.AUTHORID))
)
.where(BOOK.TITLE.eq(CLRS_title))
;
//when
Result<Record1<String>> result = query.fetch();
List<String> authorNames = result.stream().map(r-> (String)r.getValue("AuthorName")).collect(Collectors.toList());
//then
assertThat(authorNames.size(),is(4));
assertThat(authorNames.containsAll(Arrays.asList("Cormen","Leiserson","Rivest","Stein")), is(true));
在<$中强制转换为 String
c $ c> authorNames 是必需的,因为没有它,我将无法编译,因为
the cast to String
in the authorNames
generation is required because without it, I cannot compile because
不兼容的类型。必需的列表,但将'collect'推断为R:不存在类型变量的实例,因此Object符合String推断变量T具有不兼容的边界:相等约束:字符串下界:Object
Incompatible types. Required List but 'collect' was inferred to R: no instance(s) of type variable(s) exist so that Object conforms to String inference variable T has incompatible bounds: equality constraints: String lower bounds: Object
有没有办法避免这种转换并仍然获得更狭窄的选择?
Is there a way to avoid this cast and still get the more narrow select?
推荐答案
不带别名
我不明白为什么要重命名列。只是...
Without aliasing
I don't see why you rename your column. Just...
.select(AUTHOR.LASTNAME)
然后
List<String> authorNames = result.getValues(AUTHOR.LASTNAME);
带别名
如果必须使用别名,然后它有助于将别名列表达式分配给局部变量:
With aliasing
If you must alias it, then it helps assigning the aliased column expression to a local variable:
Field<String> lastname = AUTHOR.LASTNAME.as("lastname");
// ...
.select(lastname)
...然后:
List<String> authorNames = result.getValues(lastname);
或者,您可以重复两次别名表达式
Alternatively, you can repeat the aliasing expression twice
.select(AUTHOR.LASTNAME.as("lastname"))
...,然后:
List<String> authorNames = result.getValues(AUTHOR.LASTNAME.as("lastname"));
这篇关于缩小jooq选择时避免强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!