Java Hibernate为JPQL生成许多select语句

Java Hibernate为JPQL生成许多select语句,java,sql,hibernate,jpql,Java,Sql,Hibernate,Jpql,我编写了一个JPQL来获取给定客户id的ProductOrderU实体列表,但我注意到生成了许多select语句,而我只希望生成一条。例如,我使用Oracle: 从1中的客户id所在的订单中选择订单id所在的产品订单中选择*; Hibernate生成: Hibernate: select productord0_.order_id as order_id1_8_, productord0_.product_id as product_id2_8_, productord0_.amount_of_

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

从1中的客户id所在的订单中选择订单id所在的产品订单中选择*; Hibernate生成:

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_.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=?

问题是为什么?查询有问题,或者需要生成太多select语句

获取的方法:

@交易的 公共列表获取ProductOrdersLong customerId { return entityManager.createquery从ProductOrderU采购订单中选择采购订单,其中po.order.customer.customerId=:customerId,ProductOrderU.class .setParametercustomerId,customerId.getResultList; } ProductOrderU是其他几个实体的父实体:

@实体 @Tablename=产品\订单 @塞特 @吸气剂 公共类产品订单 { @嵌入ID private ProductOrderIdU productOrderId=new ProductOrderIdU; 私人长期投资于零售产品; @ManyToOneoptional=false,fetch=FetchType.LAZY @MapsIdproductId @JoinColumnname=product\u id,null=false 私人产品; @ManyToOneoptional=false,fetch=FetchType.LAZY @MapsIdorderId @JoinColumnname=order\u id,nullable=false 私人订单; } 以及这些关系:

您能像这样使用内部联接吗?请参见从产品订单p、订单o中选择*,其中p.order\U id=o.order\U id和o.customer\U id在1中;子查询可以在内部创建许多查询。从加载整个ProductOrder对象图的角度来看,生成的SQL看起来相当稳定。JPA实现通常有一种方法来控制一/多对一/多关系是否与实体同时加载,例如连接、内部连接、获取类型、获取计划。。。其配置很难调整,有时您需要快速抓取,有时则不需要。但总而言之,如果请求接近完整的对象图,那么您显示的SQL是合理的。@从ProductOrder的角度来看,需要GPI完整对象图加载,即产品、类别、顺序等。但是我不明白为什么hibernate会为此生成几个select语句。他不能用一个select来获取那些实体吗?如果你数一数,他会做9次选择,对象图包含9个实体,所以他会对每个实体进行一次选择。好吧,你的问题不是为什么,而是如何。您应该查看获取策略,例如。