Based on previous tips that I got here. I’m trying to initialize multiples @OneToMany List and I getting some errors.
I have six @OneToMany Lists in my parent entity. So, I fetch the parent with one of them.
And the rest I’m using Hibernate.initialize(obj);
But one of them I need to fetch and also fetch to initialize sub entities. So if I use Hibernate.initialize on it, this sub entity won’t initialize. So I tried to do this:
pessoa.getObservacoes().clear();
pessoa.getObservacoes().addAll(getEntityManager().createQuery(
"SELECT po FROM PessoaObservacao po " +
"JOIN FETCH po.usuario " +
"WHERE po.pessoa.id = :pessoaId", PessoaObservacao.class)
.setParameter("pessoaId", pessoa.getId())
.getResultList());
I found this as a solution in Stack Overflow, but in my method I’m using @Transactional from Java EE 7.
When I run this, addAll() is deleting the data, what’s the correct way to initialize this collection?
Clearing the collections and reconstructing it based on a query looks like a hack.
Now, the only time when you need to initialize an association is if you plan on modifying it. Having to initialize 6 collections sounds like you are trying to fetch more than necessary. Do you really need everything initialized because you want to modify the structure?
Anyway, if you don’t need to modify the structure just fetch all the needed collections using queries and populate a DTO with them.
User user = /** Parent with one of the Collections **/
Hibernate.initialize(user.getGrapevines());
user.getGrapevines().forEach(grapevine -> Hibernate.initialize(grapevine.getGrapes()));
But I test and it give me 1 Query to the “Grapevines” and 1 Query per different “Grapes” and it doesn’t sounds good.
Yes, you’re completely right this is the best scenario ever.
But even loading data in the associated Tab, how could I set the Collection in parent Entity? Is using the clear() and addAll()?
The method that will load data in a specific tab won’t have the @Transactional, but when I did this in a @Transactional method, it deletes the data in addAll().
Only you can know if you need it or not. My suggestion was to question the need for mapping collections as a mandatory requirement. If you don’t need to propagate changes from parent to children via the collection, then you might do just fine without mapping the collection.