-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I have a question about using subqueries. #819
Comments
Hi @LEEJaeHyeok97. How can we help? Do you want to know how to use subquery? Without telling me how you want to write your query in JPQL or SQL, I'm afraid the only help I can give you is a link to the subquery documentation. https://kotlin-jdsl.gitbook.io/docs/jpql-with-kotlin-jdsl/subqueries |
@shouwn |
I'm not sure I understand the requirements exactly, but here's how I would write it if I were you. Firstly, I would split the query into 2 parts. The first query is the one that extracts the ID of the target to be looked up. The reason for doing this is to prevent LazyJoin causing unexpected performance issues if CafeEntity and TagEntity have a relationship such as OneToOne. And I wouldn't use subqueries, because I think subqueries should really be a last resort. And I would add a limit when I call the DB query. If I had a fetch join, I wouldn't be able to add a limit because I would be doing the join in memory. But if we split the query, we can add a limit. Here's how I reorganised your query. It won't compile. override fun findAllCafesById(lastCafeId: UUID?, limit: Int): CafePage {
val cafeIdsQuery = jpql {
select<UUID>(
path(CafeEntity::id),
).from(
entity(CafeEntity::class),
).whereAnd(
path(CafeEntity::deletedAt).isNull(),
lastCafeId?.let { path(CafeEntity::id).greaterThan(it) },
).orderBy(
path(CafeEntity::id).asc()
)
}
val cafeIds: List<UUID> = entityManager
.createQuery(cafeIdsQuery, renderContext)
.apply {
maxResults = limit + 1
}.resultList
val cafes: Map<UUID, CafeEntity> = TODO("load cafes from cafeIds")
val tags: Map<UUID, List<TagEntity>> = TODO("load tags from cafeIds")
val cafesWithTags = cafeIds.map {
val cafe = cafes[it]
val tags = tags[it]
CafeInfoWithTags.of(cafe, tags)
}
val hasNext = cafeIds.size > limit
return CafePage.from(
SliceImpl(
cafesWithTags,
Pageable.unpaged(), hasNext,
),
)
} |
In the code below, I want to exclude deleted cafe information from the query.
I'm trying to use a subquery to filter out the deleted cafe information in my query, and I need some assistance with this.
Have a great day!
The text was updated successfully, but these errors were encountered: