I am trying to load single Merchant entity with its Categories, and relationship between Merchant and Category is Many-To-Many. Also Category entity has also translations... So I wrote following code to retrieve data that I need from database:
Merchant merchant = queryFactory.selectFrom(QMerchant.merchant)
.leftJoin(QMerchant.merchant.categories, QMerchantCategory.merchantCategory).fetchJoin()
.leftJoin(QMerchantCategory.merchantCategory.translations, QMerchantCategoryTranslation.merchantCategoryTranslation).fetchJoin()
.where(QMerchant.merchant.id.eq(merchantStore.getMerchant().getId()))
.fetchOne();
However, what I get as a result is Merchant that has list of three categories (that are same) and each with three translation objects...
I can check in database that there is only one category in replationship with merchant that I am trying to load, and as you can see from the picture from JVM IDs that those are all same instances... Also following code gives same result:
@EntityGraph(attributePaths = {"categories", "categories.translations"})
Optional<Merchant> findWithCategoriesById(long id);
If add another translation then I will have merchant with four categories...
So how do I tell hibernate to load Merchant entity that has one category entity in the list (as it should have, because there is only one category associated with merchant), but I also need all translations to be loaded for that category, and if possible, to do it with one query, i can solve it with two queries, but i would like it to be only one... or if there is a better reason to have two queries to extract data from database...?