1

I have @ManyToMany relationship between two of my entities. When I try to load the parents with their children, the parents repeat in response payload. I want to solve my problem with EntityGraphs.

Here is the parent with annotation.

@Entity
@Table(name="Parent_table")
@NamedEntityGraph(
    name = "Parent.children",
    attributeNodes = @NamedAttributeNode("children"))

public class Parent implements Serializable{

//some extra code

@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
        name="join table", joinColumns=@JoinColumn(name="key"),inverseJoinColumns=@JoinColumn(name="key"))
private List<Child> children;

Here is the code in repository for parent.

    @EntityGraph(value = "Parent.children", type = EntityGraphType.LOAD)
public List<Parent> findAll(Predicate predicate);

I have many to many relationship between two of my entities. But I want to get result like

parent1 {
child1,
child2
}

But I am getting parents for each combination..

Parent1 {
child1,
child2
}

Parent1 {
child1,
child2
}

there I get parent2.

But what I want is to get the parent1 only once, not repeating. While using entity graph.

Sagar Mishra
  • 11
  • 1
  • 3
  • *the parents repeat in response payload.* What so you mean by "response payload"? If you're talking about a JSON representation then `@EntityGraph` isn't going to help you with that. – Alan Hay Jan 31 '19 at 12:00
  • By response payload, I mean the output that I am getting as JSON. I get each combination of parents multiple times(equal to the number of children) – Sagar Mishra Jan 31 '19 at 17:58

1 Answers1

1

If you use an entity graph, Hibernate joins the parent and the child table in the query. This creates a product in the result set and you get a reference to the parent entity for each of its children.

You can fix using the DISTINCT keyword in your query. With Spring Data JPA, you can do that in 2 ways:

  1. You can change the method name from findAll to findAllDistinctBy.
  2. You can define your own query using @Query("SELECT DISTINCT p FROM parent p")

Hibernate will include the DISTINCT keyword in your SQL query. You can avoid that by setting the query hint QueryHints.HINT_PASS_DISTINCT_THROUGH to false.

Thorben Janssen
  • 3,012
  • 9
  • 23
  • No property findDistinctAll found for type ServiceBundle! I am getting this when I am trying to use findDistinctAll – Sagar Mishra Jan 31 '19 at 17:49
  • Sorry, my mistake. It has to be findAllDistinctBy. I also updated my response. – Thorben Janssen Jan 31 '19 at 17:58
  • Your blogs did help me out so much and thanks for all those material you provide at your website. BTW public List listAll() { Predicate p = null; Long start = System.currentTimeMillis(); List lst = repo.findAllDistinctBy(p); log.debug("DB Time to list parents = " + (System.currentTimeMillis() - start)); return lst; } what should I do to list all parents only once? – Sagar Mishra Jan 31 '19 at 18:17
  • I don't understand your question. Doesn't your code return a List that contains all parents only once? – Thorben Janssen Jan 31 '19 at 18:25
  • I have edited the question. Please look at added information in the end. – Sagar Mishra Feb 04 '19 at 04:59