Spring data: combine @Query with characteristics

I have two objects:

@Entity
public class FirstEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    @Size(min = 1, max = 50)
    private String title;

    @Column(nullable = true)
    private String description;

    @OneToMany(mappedBy = "firstEntity")
    private Set<SecondEntity> secondEntities = new HashSet<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<SecondEntity> getSecondEntities() {
        return secondEntities;
    }

    public void setSecondEntities(Set<SecondEntity> secondEntities) {
        this.secondEntities = secondEntities;
    }
}

      

and

@Entity
public class SecondEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne
    private FirstEntity firstEntity;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public FirstEntity getFirstEntity() {
        return firstEntity;
    }

    public void setFirstEntity(FirstEntity firstEntity) {
        this.firstEntity = firstEntity;
    }
}

      

I have one repository:

@Repository
public interface FirstRepository extends JpaRepository<FirstEntity, Long>,
        JpaSpecificationExecutor<FirstEntity> {
    @Query("SELECT e.id, e.title, COUNT(*) " +
            "FROM myPackage.FirstEntity e " +
            "LEFT JOIN e.secondEntities " +
            "GROUP BY e.id, e.title")
    List<Object[]> getData();
}

      

I would like to add specyfications to the above method, so I modify the original code like this:

@Repository
public interface FirstRepository extends JpaRepository<FirstEntity, Long>,
        JpaSpecificationExecutor<FirstEntity> {
    @Query("SELECT e.id, e.title, COUNT(*) " +
            "FROM myPackage.FirstEntity e " +
            "LEFT JOIN e.secondEntities " +
            "GROUP BY e.id, e.title")
    List<Object[]> getData(Specifications<FirstEntity> specifications);
}

      

But when I call the getData method, I see the following exception: java.lang.IllegalArgumentException: Parameter with that position [1] did not exist

Can I combine JPQL query with BOMs?

+3


source to share





All Articles