微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Hibernate为JPQL生成许多select语句

如何解决Hibernate为JPQL生成许多select语句

我写了一个JPQL来获取给定客户ID的ProductOrderU实体的列表,但是我注意到生成了许多select语句,而我只希望其中一个。例如(我使用Oracle):

select * from product_order where order_id in (select order_id from orders where customer_id in (1));

休眠生成

Hibernate: select productord0_.order_id as order_id1_8_,productord0_.product_id as product_id2_8_,productord0_.amount_of_ordered_products as amount_of_ordered_3_8_ from product_order productord0_ cross join orders orderu1_ where productord0_.order_id=orderu1_.order_id and orderu1_.customer_id=?
Hibernate: select orderu0_.order_id as order_id1_6_0_,orderu0_.cost_of_delivery as cost_of_delivery2_6_0_,orderu0_.cost_of_products as cost_of_products3_6_0_,orderu0_.customer_id as customer_id5_6_0_,orderu0_.delivery_id as delivery_id6_6_0_,orderu0_.final_cost as final_cost4_6_0_ from orders orderu0_ where orderu0_.order_id=?
Hibernate: select productu0_.product_id as product_id1_7_0_,productu0_.amount_in_stock as amount_in_stock2_7_0_,productu0_.category_id as category_id11_7_0_,productu0_.description as description3_7_0_,productu0_.product_name as product_name4_7_0_,productu0_.product_price as product_price5_7_0_,productu0_.production_year as production_year6_7_0_,productu0_.promotion_id as promotion_id12_7_0_,productu0_."size" as size7_7_0_,productu0_.sold_amount as sold_amount8_7_0_,productu0_.vendor as vendor9_7_0_,productu0_.weight as weight10_7_0_ from product productu0_ where productu0_.product_id=?
Hibernate: select complaints0_.order_id as order_id1_2_0_,complaints0_.complaint_id as complaint_id2_2_0_,complaintu1_.complaint_id as complaint_id1_1_1_,complaintu1_.complaint_time as complaint_time2_1_1_,complaintu1_.content as content3_1_1_,complaintu1_.handling_time as handling_time4_1_1_ from complaints_for_orders complaints0_ inner join complaint complaintu1_ on complaints0_.complaint_id=complaintu1_.complaint_id where complaints0_.order_id=?
Hibernate: select customeru0_.customer_id as customer_id1_3_0_,customeru0_.city as city2_3_0_,customeru0_.email as email3_3_0_,customeru0_.first_name as first_name4_3_0_,customeru0_.flat_number as flat_number5_3_0_,customeru0_.house_number as house_number6_3_0_,customeru0_.password as password7_3_0_,customeru0_.phone_number as phone_number8_3_0_,customeru0_.postal_code as postal_code9_3_0_,customeru0_.street as street10_3_0_,customeru0_.surname as surname11_3_0_ from customer customeru0_ where customeru0_.customer_id=?
Hibernate: select deliveryu0_.delivery_id as delivery_id1_4_0_,deliveryu0_.delivery_company_id as delivery_company_i4_4_0_,deliveryu0_.delivery_time as delivery_time2_4_0_,deliveryu0_.price as price3_4_0_,deliveryu0_.promotion_id as promotion_id5_4_0_ from delivery deliveryu0_ where deliveryu0_.delivery_id=?
Hibernate: select deliveryco0_.delivery_company_id as delivery_company_i1_5_0_,deliveryco0_.delivery_company_name as delivery_company_n2_5_0_ from delivery_company deliveryco0_ where deliveryco0_.delivery_company_id=?
Hibernate: select categoryu0_.category_id as category_id1_0_0_,categoryu0_.category_name as category_name2_0_0_,categoryu0_.parent_category_id as parent_category_id3_0_0_,categoryu0_.promotion_id as promotion_id4_0_0_ from category categoryu0_ where categoryu0_.category_id=?
Hibernate: select categoryu0_.category_id as category_id1_0_0_,categoryu0_.promotion_id as promotion_id4_0_0_ from category categoryu0_ where categoryu0_.category_id=?

问题是为什么?查询有问题,或者只需要生成许多选择语句即可。

获取方法

@Transactional
public List<ProductOrderU> fetchProductOrders(Long customerId)
{
   return entityManager.createquery("select po from ProductOrderU po where po.order.customer.customerId = :customerId",ProductOrderU.class)
                .setParameter("customerId",customerId).getResultList();
}

ProductOrderU是其他几个的父实体:

@Entity
@Table(name = "product_order")
@Setter
@Getter
public class ProductOrderU
{
    @EmbeddedId
    private ProductOrderIdU productOrderId = new ProductOrderIdU();

    private Long amountOfOrderedProducts;

    @ManyToOne(optional = false,fetch = FetchType.LAZY)
    @MapsId("productId")
    @JoinColumn(name = "product_id",nullable = false)
    private ProductU product;

    @ManyToOne(optional = false,fetch = FetchType.LAZY)
    @MapsId("orderId")
    @JoinColumn(name = "order_id",nullable = false)
    private OrderU order;
}

与关系:

Relationship

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。