First of all, I already tried this JDBI 3: Nested SQLObject and it didn't work
I'm trying basically the same thing as the other person, to gather some nested objects in jdbi 3 but using RegisterConstructorMapper
instead.
This is my code:
ClassA.java
@Value
@AllArgsConstructor
@Jacksonized
@Builder(toBuilder = true)
public class ClassA {
@Nested
ClassB classB;
String someString;
}
ClassB.java
@Value
@AllArgsConstructor
@Jacksonized
@Builder(toBuilder = true)
public class ClassB {
Long id;
}
The code in my dao:
@SqlQuery("""
SELECT
a.some_string as someString,
b.id as b_id
FROM table_a a
INNER JOIN table_b b on a.b_id = b.id
WHERE a.id = :id
""")
@RegisterConstructorMapper(value = ClassB.class, prefix = "b")
@RegisterConstructorMapper(ClassA.class)
Optional<ClassA> findClassA(long id);
But when I run the code I keep getting this error:
Instance factory 'public com.blah.ClassA(com.blah.ClassB,java.lang.String)' parameter '[classB]' has no matching columns in the result set. Verify that the Java compiler is configured to emit parameter names, that your result set has the columns expected, annotate the parameter names explicitly with @ColumnName, or annotate nullable parameters as @Nullable Blockquote Blockquote
What you need is a row reducer. With a join you will get the same A for every B (that is for an left join the default). You have an inner join so not sure if that is true for this case but I think your problem will be solved with a row reducer. Something like this:
and add the reducer to your method: