FetchType.EAGER not loading list of children

I’m little confused, because I’m facing an error I do not understand. Firstly, I present you simplified code of my entities.

Language.java

@Entity
@Table(name = "languages")
@NoArgsConstructor
@Data
@AllArgsConstructor
public class Language {

    @Id
    @Column(name = "id_language")
    @SequenceGenerator(name = "language_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "language_seq")
    @NotNull
    private long id;

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "id_account")
    @OnDelete(action = OnDeleteAction.CASCADE)
    @NotNull
    @JsonBackReference
    private Account account;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "language",cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    private List<CategorySetting> categorySettings;

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "language",cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    private List<Category> categories;

    @JsonProperty
    public Account getAccountJson() {
        if(account == null){
            return null;
        }

        account.setLanguages(null);

        return account;
    }
}

CategorySetting.java

@Entity
@Table(name = "categorySettings")
@NoArgsConstructor
@Data
@AllArgsConstructor
public class CategorySetting {

    @Id
    @Column(name = "id_category_setting")
    @SequenceGenerator(name = "category_setting_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_setting_seq")
    @NotNull
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name = "id_language")
    @JsonBackReference
    private Language language;
}

Category.java

@Entity
@Table(name = "categories")
@NoArgsConstructor
@Data
@AllArgsConstructor
public class Category {

    @Id
    @Column(name = "id_category")
    @SequenceGenerator(name = "category_seq")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "category_seq")
    @NotNull
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_language")
    @JsonBackReference
    private Language language;

    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(
            name = "CategoryWordConfig",
            joinColumns = { @JoinColumn(name = "id_category") },
            inverseJoinColumns = { @JoinColumn(name = "id_wordConfig") }
    )
    private List<WordConfig> wordConfigs = new ArrayList<>();

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Category category = (Category) o;
        return id.equals(category.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @JsonProperty
    public Language getLanguageJson() {
        if(language == null){
            return null;
        }

        language.setCategories(null);
        return language;
    }
}

Now – the problem is that categories are not load while I use findById.
E.g.:
First of all I use this simple method to create new instance of language:

   public Language addLanguage(Language language) {
        List<CategorySetting> categorySettings = new ArrayList<>();
        CategorySetting categorySetting = new CategorySetting(null, language);
        categorySettings.add(categorySetting);

        List<Category> categories = new ArrayList<>();
        Category category = new Category(null, language, null);
        categories.add(category);


        language.setCategorySettings(categorySettings);
        language.setCategories(categories);

        return languageRepository.save(language);

    }

All needed data are passed through parameter “language” but categorySettings and categories – these are null.
I create 2 objects, put them into lists and set to “langauge” (don’t bother with the pointlessness of the code – it is simplified).
Now I call save() method and the data are saved. When I check database (PostgreSQL) language are stored, categorySetting and category are stored too – with correct “id_language”.

findLanguageById:

@Repository
public interface LanguageRepository extends CrudRepository<Language, Long> {
    Language findLanguageById(Long id);
}

Now I use this simple method:

public Language getLanguageById(Long id) {
    Language language = languageRepository.findLanguageById(id);
    return language;
}

And now the problem occurs. Language is loaded. List of categorySettings belonging to this language too but categories are not. Why is that? The whole procedure looks the same for this lists and one of them is loaded, another not. I have no idea what can causes this trouble. May you give me some advice?

Leave a Reply

avatar
  Subscribe  
Notify of