Shashank
Shashank

Reputation: 249

NamedEntityGraph: Hibernate throwing cannot simultaneously fetch multiple bags exception for Nested collection

I am getting "cannot simultaneously fetch multiple bags" for nested collection. Here is what entity and entity graph definition looks like. Hibernate version: 4.3.7 spring-data-jpa: 1.8.1 '

@Entity
@Table(name = "ORGANISATION_GROUP")
@Where(clause = "IS_DELETED <> 1")
@NamedEntityGraphs(value = { @NamedEntityGraph(name = "onlyOrganisations", attributeNodes = { @NamedAttributeNode(value = "organisations", subgraph = "lightOrganisation") },
        subgraphs = { @NamedSubgraph(name = "lightOrganisation", attributeNodes = { @NamedAttributeNode(value = "locations") }) }) })
public class OrganisationGroup implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    private Long id;

    @ManyToMany()
    @AuditJoinTable(name = "ORGANISATION_GROUP_MEMBER_AUD")
    @JoinTable(name = "ORGANISATION_GROUP_MEMBER", joinColumns = @JoinColumn(name = "ORGANISATION_GROUP_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(
            name = "ORGANISATION_ID", referencedColumnName = "ID"))
    private List<Organisation> organisations = new ArrayList<>(0);

}

@Entity
@Table(name = "ORGANISATION")
public class Organisation implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private Long id;

    @Column(name = "name", length = 255, nullable = false)
    private String name;

    @OneToMany( mappedBy = "organisationId", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy(value = "location ASC")
    private List<OrganisationLocation> locations;

}

@Entity
@Table(name = "ORGANISATION_LOCATIONS")
public class OrganisationLocation implements Serializable {
    private static final long serialVersionUID = 1L;

   @Id
   private Long id;

   @Column(name = "LOCATION")
   private String location;

 }

` I tried to use @LazyCollection on locations attribute but still getting same exception. However, the same thing works without entity graph and making locations as fetch eager type. How to resolve this error using entity graph?

Upvotes: 1

Views: 2189

Answers (1)

David Levesque
David Levesque

Reputation: 22451

Unless you have a good reason not to do so, try changing your collections to use Set instead of List.

Upvotes: 3

Related Questions