具有一对多关系的JPA查询

问题描述 投票:0回答:1

我有两个实体A和B,如下所示:

@Entity
public class A {
   @Id
   private int id;
   private String name;
   private boolean hasFailedChild;

   @OneToMany
   private List<B> bs;

   public A(String name, long count) {
        this.name = name;
        this.hasFailedChild = (count > 0);
   }
}

@Entity
public class B {
   @id
   private int id;
   private String name;
   private String status;
   @ManyToOne
   private A a;

   //required constructors
}

我正在尝试使用@Query以A状态获取所有B及其计数Failed

我已经尝试过以下查询:

public interface ARepository extends CrudRepository<A, Integer> {
@Query(value = "select new A(a.name, count(b)) " +
        "from A a left join a.bs b where b.status = 'Failed'")
       List<A> findAllA();
}

但是,它不起作用。有人可以在这里帮助我。

hibernate spring-boot spring-data-jpa jpa-2.0 jpa-2.1
1个回答
0
投票

您应通过以下方式更正您的查询:

© www.soinside.com 2019 - 2024. All rights reserved.