OneToMany with query dsl and dto pattern problem

ぐ巨炮叔叔 提交于 2020-08-06 07:09:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!