Reputation: 2268
I'm using @NamedEntityGraph annotation to load a graph from database.
@NamedEntityGraph(
name = "Firma.uredjivanje",
attributeNodes = {
@NamedAttributeNode(value="prevodi", subgraph = "prevodi")
},
subgraphs = {
@NamedSubgraph(
name = "prevodi",
attributeNodes = {
@NamedAttributeNode(value = "jezik", subgraph = "jezik")
}
)
}
)
In the Spring Data JPA repository, I'm using annotation:
@EntityGraph(value="Firma.uredjivanje", type = EntityGraph.EntityGraphType.LOAD)
List<Firma> getByAktivna(boolean aktivna);
Everything works as expected, expect that all relations are joined, and I get duplicate Firma entities (because of JOIN). Instead of a List with entity id's {1,2,3}, I get {1,1,1,2,2,3}.
What is the best way to get distinct entities (if this is not a bug ofcourse).
Upvotes: 8
Views: 7619
Reputation: 493
After adding dictinct
I have error logs during server starup (maybe because there is no such thing as findDistinctAll()
.
For me help a hint in https://jira.spring.io/browse/DATAJPA-680.
I added @Query
annotation to the method, so my method looks like below
@EntityGraph(value = "User.detail", type = EntityGraph.EntityGraphType.LOAD)
@Query(value = "SELECT DISTINCT u FROM User u")
List<User> findAll();
Upvotes: 0
Reputation: 2268
Found the answer... Since NamedEntityGraph does JOIN in database, it selects all entities without DISTINCT. So the solution is to use Distinct in method name...
@EntityGraph(value="Firma.uredjivanje", type = EntityGraph.EntityGraphType.LOAD)
List<Firma> getDistinctByAktivna(boolean aktivna);
Upvotes: 10