Table names or aliases as prefix to resolve columns of joined tables
#2292 aperta il 13 mar 2023
Metriche repository
- Star
- (1745 stelle)
- Metriche merge PR
- (Merge medio 1g 10h) (6 PR mergiate in 30 g)
Descrizione
Hello!
I wonder if it is possible, using JDBI, to use table names as prefixes for column disambiguation.
For example: I have two Java classes:
class Inner {
String id;
String name;
}
class Outer {
String id;
String value;
@Nested
Inner inner;
}
In my DB, I have two tables:
CREATE TABLE inner(id VARCHAR, name VARCHAR);
CREATE TABLE outer(id VARCHAR, value VARCHAR, inner_id VARCHAR);
I have an OuterDao class that does a simple SELECT operation:
@SqlQuery("SELECT * FROM outer LEFT JOIN inner ON (outer.inner_id = inner.id) LIMIT 1;")
@RegisterConstructorMapper(value = Optional.class)
Optional<Outer> selectOne();
This does not work as there is a matching column name id between the two tables. One solution proposed by JDBI is to set a prefix for the nested entity and then alias all the columns from the nested entity.
Is there a way to use the table names or even table aliases as prefix? This would avoid the 'verbosity' of listing all columns from the inner table and write a smaller query.
For example in the Outer class we could have
@Nested("inner")
Inner inner;
or
@Nested("inner.")
Inner inner
Thanks for the support!