Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Group By和Having子句为多对多关系编写联接查询_Java_Hibernate_Spring Boot_Spring Data Jpa - Fatal编程技术网

Java 使用Group By和Having子句为多对多关系编写联接查询

Java 使用Group By和Having子句为多对多关系编写联接查询,java,hibernate,spring-boot,spring-data-jpa,Java,Hibernate,Spring Boot,Spring Data Jpa,我需要返回与订单关联的超过2个产品的每个订单。订单和产品具有多对多关系,如下所示 @Data @Entity @Table(name = "orders") @JsonInclude(NON_NULL) public class Order implements Serializable { public static final int PRECISION = 2; @Id @GeneratedValue(strategy = IDENTITY) @Json

我需要返回与订单关联的超过2个产品的每个订单。订单和产品具有多对多关系,如下所示

@Data
@Entity
@Table(name = "orders")
@JsonInclude(NON_NULL)
public class Order implements Serializable {

    public static final int PRECISION = 2;

    @Id
    @GeneratedValue(strategy = IDENTITY)
    @JsonIgnore
    private String orderId;

    . . .  Other fields . . . .

    @ManyToMany(fetch = EAGER, cascade = ALL)
    @JoinTable(
            name = "order_product",
            joinColumns = @JoinColumn(name = "order_id", updatable = false, nullable = false),
            inverseJoinColumns = @JoinColumn(name = "product_id", updatable = false, nullable = false)
    )
    private List<Product> products = new ArrayList<>();
}
我的SQL查询应该是

SELECT o.order_id, p.product_id from Orders o join 
    OrderProduct op on o.order_id = op.order_id join Product p on   
    op.product_id = p.product_id group by o.order_id, p.product_id having    
    (p.product_id) > 2
我写这个查询的最佳尝试

   @Query("SELECT o from Order o JOIN o.products where o.id = :id GROUP BY o HAVING (o.product) > 2")
  List<Order> findOrderWithMultipleProducts();
控制器仅直接调用此函数。所需的JSON负载如下所示

{
         "orderId": "order_id",
         "products": [
             {
                 "productId": "product_id_1",
             },
             {
                  "productId": "product_id_2",
             },
             {
                  "productId": "product_id_3",
             }
         ]
     }

查询的where子句为:id,带有:字符。所以hibernate需要一个参数

  @Query("SELECT o from Order o JOIN o.products where o.id = :id GROUP BY o HAVING (o.product) > 2")
  List<Order> findOrderWithMultipleProducts();
假设id是字符串类型

使用@Query注释的另一种方法是在方法中传递参数,并更改:id以反映参数序列

@Query("SELECT o from Order o JOIN o.products where o.id = ?1 GROUP BY o HAVING (o.product) > 2")
List<Order> findOrderWithMultipleProducts(int id);
@Query(“从订单中选择o加入o.products,其中o.id=?1个组,o拥有(o.product)>2”)
列出具有多个产品(int id)的FindOrders;
更多详细信息可在此处找到:

我不知道这个问题的答案,但OP在加入某个领域时不需要“where”子句。
  @Query("SELECT o from Order o JOIN o.products where o.id = :id GROUP BY o HAVING (o.product) > 2")
  List<Order> findOrderWithMultipleProducts();
SQLQuery query = SELECT o from Order o JOIN o.products where o.id = :id GROUP BY o HAVING (o.product) > 2
query.setString("id",ValueOfId);
@Query("SELECT o from Order o JOIN o.products where o.id = ?1 GROUP BY o HAVING (o.product) > 2")
List<Order> findOrderWithMultipleProducts(int id);