1

I am ussing micronaut-data framework to a app that store server emails.

I have a entity EMAILDB, that store the email in sql tables.

in this entity i have many to many relations with email address:

@ManyToMany
List<AddressDB> destinatarysTo;
@ManyToMany
List<AddressDB> destinatarysCC;
@ManyToMany
List<AddressDB> destinatarysCCO;

And i have a method that give me this emails in the email repository:

@Join(value = "destinatarysTo", type = Join.Type.LEFT_FETCH)
@Join(value = "destinatarysCC", type = Join.Type.LEFT_FETCH)
@Join(value = "destinatarysCCO", type = Join.Type.LEFT_FETCH)
public List<EmailDB> findByFolder(FolderDB folder);

But when i use it:

java.lang.IllegalArgumentException: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags

What is the correct way?

thanks

mls_dev
  • 511
  • 2
  • 6
  • 21
  • Does this answer your question? [Hibernate throws MultipleBagFetchException - cannot simultaneously fetch multiple bags](https://stackoverflow.com/questions/4334970/hibernate-throws-multiplebagfetchexception-cannot-simultaneously-fetch-multipl) – SternK Mar 15 '21 at 08:53
  • I am not sure, the solution proposed in that question is use @LazyCollection(LazyCollectionOption.FALSE) , but micronaut is not a complete jpa or hibernaute ORM, it always fetch lazy. for avoid n+1 query – mls_dev Mar 15 '21 at 08:57
  • The root of the problem here is that you try to fetch several collections simultaneously. The main goal of this exception is notify you about potential problem with Cartesian Product. As it's suggested in [this](https://stackoverflow.com/a/51055523/6277104) answer: *As long as you fetch at most one collection using JOIN FETCH, you will be fine.* – SternK Mar 15 '21 at 10:00
  • thanks i don't find the way for solution , and i remove joins fetch – mls_dev Mar 16 '21 at 09:16
  • As workaround you can replace `List` by `Set`, if Cartesian Product is not important for this case. – SternK Mar 16 '21 at 09:44

0 Answers0