pramoth / specification-with-projection

Support projections with JpaSpecificationExecutor.findAll(Specification,Pageable) for Spring Data JPA
MIT License
164 stars 56 forks source link

Returning repeated values when target entity has OneToMany relationship #28

Open genndi opened 1 year ago

genndi commented 1 year ago

JpaSpecificationExecutorWithProjection.findAll(Specification spec, Class projectionType, Pageable pageable) returns duplicates

my entity:

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

        private String name;

        @OneToMany
        private Set<B> list;
....
}
public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

        private String name;

        @ManyToOne
        private A resource;
....
}
public interface SummaryA{
       public String getName();
       public Set<SummaryB> getList();

      interface SummaryB {
            public String getName();
     }
}

when I run the search with aRepositoy.findAll I get the following result:

ID   |    NAME      |    sizeOfB
--------------------------------------
1    |    resource1 |          1
--------------------------------------
1    |    resource1 |          1
--------------------------------------
2    |    resource2 |          1
--------------------------------------
2    |    resource2 |          1
--------------------------------------
3    |    resource3 |          1
--------------------------------------

but the expected result was the following:

ID   |    NAME      |    sizeOfB
--------------------------------------
1    |    resource1 |          2
--------------------------------------
2    |    resource2 |          2
--------------------------------------
3    |    resource3 |          1
--------------------------------------