2

I have two entities A and B as below:

@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
}

I am trying to get all As along with the its Bs count with Failed status using @Query.

I have tried with the below Query:

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();
}

However, it is not working. Can someone help me here.

2
  • You have a default constructor for A, don't you? Commented Mar 4, 2020 at 16:45
  • Yes, I have. If I need to create any other constructors also, I can create them. Commented Mar 4, 2020 at 16:59

1 Answer 1

1

You should correct your query in the following way:

select new A(a.name, count(b))
from A a
left join a.bs b
where b.status = 'Failed'
group by a.name

See the documentation for additional details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.