2.2 More Associations
Query Dynamic Entities
- Java
- Kotlin
BookTable table = BookTable.$;
List<Book> books = sqlClient
.createQuery(table)
.where(table.name().eq("Learning GraphQL"))
.orderBy(table.edition().desc())
.select(
table.fetch(
BookFetcher.$
.allScalarFields()
.store( ❶
BookStoreFetcher.$
.allScalarFields()
)
.authors( ❷
AuthorFetcher.$
.allScalarFields()
)
)
)
.execute();
val books = sqlClient
.createQuery(Book::class) {
where(table.name eq "Learning GraphQL")
orderBy(table.edition.desc())
select(
table.fetchBy {
allScalarFields()
store { ❶
allScalarFields()
}
authors { ❷
allScalarFields()
}
}
)
}
.execute()
The result is
[
{
"id":3,
"name":"Learning GraphQL",
"edition":3,
"price":51,
"store":{ ❶
"id":1,
"name":"O'REILLY",
"website":null
},
"authors":[ ❷
{
"id":2,
"firstName":"Alex",
"lastName":"Banks",
"gender":"MALE"
},
{
"id":1,
"firstName":"Eve",
"lastName":"Procello",
"gender":"FEMALE"
}
]
},
{
"id":2,
"name":"Learning GraphQL",
"edition":2,
"price":55,
"store":{ ❶
"id":1,
"name":"O'REILLY",
"website":null
},
"authors":[ ❷
{
"id":2,
"firstName":"Alex",
"lastName":"Banks",
"gender":"MALE"
},
{
"id":1,
"firstName":"Eve",
"lastName":"Procello",
"gender":"FEMALE"
}
]
},
{
"id":1,
"name":"Learning GraphQL",
"edition":1,
"price":50,
"store":{ ❶
"id":1,
"name":"O'REILLY",
"website":null
},
"authors":[ ❷
{
"id":2,
"firstName":"Alex",
"lastName":"Banks",
"gender":"MALE"
},
{
"id":1,
"firstName":"Eve",
"lastName":"Procello",
"gender":"FEMALE"
}
]
}
]
Query Static DTO
Create any file with the dto
extension under the src/main/dto
folder, and edit the code as follows:
export com.yourcompany.yourproject.model.Book
-> package com.yourcompany.yourproject.model.dto
BookView {
#allScalars(this)
store { ❶
#allScalars(this)
}
authors { ❷
#allScalars(this)
}
}
Compile the project, a java/kotlin class named BookView
will be automatically generated.
- Java
- Kotlin
BookTable table = BookTable.$;
List<BookView> books = sqlClient
.createQuery(table)
.where(table.name().eq("Learning GraphQL"))
.orderBy(table.edition().desc())
.select(
table.fetch(BookView.class)
)
.execute();
val books = sqlClient
.createQuery(Book::class) {
where(table.name eq "Learning GraphQL")
orderBy(table.edition.desc())
select(
table.fetch(BookView::class)
)
}
.execute()
The result is
[
BookView(
id=1,
name=Learning GraphQL,
edition=1,
price=50.00,
store=BookView.TargetOf_store( ❶
id=1,
name=O'REILLY,
website=null
),
authors=[ ❷
BookView.TargetOf_authors(
id=2,
firstName=Alex,
lastName=Banks,
gender=MALE
),
BookView.TargetOf_authors(
id=1,
firstName=Eve,
lastName=Procello,
gender=FEMALE
)
]
),
BookView(
id=2,
name=Learning GraphQL,
edition=2,
price=55.00,
store=BookView.TargetOf_store( ❶
id=1,
name=O'REILLY,
website=null
),
authors=[ ❷
BookView.TargetOf_authors(
id=2,
firstName=Alex,
lastName=Banks,
gender=MALE
),
BookView.TargetOf_authors(
id=1,
firstName=Eve,
lastName=Procello,
gender=FEMALE
)
]
),
BookView(
id=3,
name=Learning GraphQL,
edition=3,
price=51.00,
store=BookView.TargetOf_store( ❶
id=1,
name=O'REILLY,
website=null
),
authors=[ ❷
BookView.TargetOf_authors(
id=2,
firstName=Alex,
lastName=Banks,
gender=MALE
),
BookView.TargetOf_authors(
id=1,
firstName=Eve,
lastName=Procello,
gender=FEMALE
)
]
)
]