I use spring boot 2.1 with spring data jpa and hibernate
@Entity
public class Factories{
@Id
@SequenceGenerator(name = "factories_id_seq", sequenceName = "factories_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "factories_id_seq")
private Integer id;
@OneToMany(mappedBy = "factory", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private List<Machines> machines = new ArrayList<>();
@ElementCollection(fetch = FetchType.LAZY)
private Set<String> emails = new HashSet<>();
}
In my machine entity, i have an equals and hashCode method.
Query done
SELECT distinct f from Factories f "
+ "LEFT JOIN FETCH f.machines "
+ "Left JOIN FETCH f.emails "
+ "Left JOIN FETCH f.cities "
+ "where f.id=:id
I tried with distinct and without
My factory with id 1
have 3 machine and 3 emails
Instead of loading factory with 3 machines and 3 emails, I get 9 machines. In the database there are only 3.
It's like a cartesian product is done.
Any idea