Let's say I have a book database and I want to check whether the CLRS book has the correct authors.
Assuming private static final String CLRS_title = "Introduction to Algorithms";
@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.)
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));
the cast to String
in the authorNames
generation is required because without it, I cannot compile because
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?