In @PostConstruct
method I fetch the parent entity joining with one of the collections and initialize the rest of them using Hibernate.initialize(obj)
, but some of them have relations that I need to fetch too.
Person person = /** SELECT Person JOINING WITH ADDRESS, CITY AND THE REST **/
Hibernate.initialize(person.getEmails());
Now I have to initialize the Collection @OneToMany Phones(), but inside Phones() I have PhoneType entity:
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "tipo_id", nullable = false)
private PhoneType type;
If I just Hibernate.initialize(person.phones()), type won't be loaded.
I found a "solution" in other topic: https://stackoverflow.com/a/47611257/8107849
That is initialize phones() and forEach all phones() initializing the type, like this:
Hibernate.initialize(person.getPhones());
person.getPhones().forEach(phone -> Hibernate.initialize(phone.getType()));
But analyzing the queries, it gave to me one query to getPhones() and one query per different Type(). In my case, five queries.
Is there any way to fetch this situation with one query?
Thanks in advance