问题
i have below entities
Cart(id, cartNumber) - one Cart has many CartDetail
CartDetail(id, quantity, total, cartId, productId)
Product(id, name) - one Product has many CartDetail
i also have corresponding DTO classes
now using QueryDSL with DTO pattern i need to find out cart by id with following fields cartId, cartNumber, cartDetails(quantity, total, productName)
currently this i am doing like below
JPQLQuery<CartDto> jpqlQuery = query.select(new QCartDto(cart.id, cart.cartNumber, cart.cartDateTime))
.from(cart).where(cart.id.eq(id));
CartDto cartResult = jpqlQuery.fetchOne();
if (!MethodUtils.isObjectisNullOrEmpty(cartResult)) {
JPQLQuery<CartDetailDto> subQuery = query.select(new QCartDetailDto(cartdetail.id, cartdetail.quantity, cartdetail.total, cartdetail.product.id,
cartdetail.product.name)).from(cartdetail).where(cartdetail.cart.id.eq(id));
List<CartDetailDto> cartDetails = subQuery.fetch();
cartResult.setCartDetails(cartDetails);
}
Problem is it fires two queries one for cart and another for cartdetail can we do this in single join query ? and get same result ?
来源:https://stackoverflow.com/questions/62863295/onetomany-with-query-dsl-and-dto-pattern-problem