Hello guys
I have the class Articulo with the follow code
Code:
@OneToMany(mappedBy="articulo", cascade = CascadeType.ALL, orphanRemoval=true)
public Set<ComprobanteDetalle> getComprobanteDetalles() {
return comprobanteDetalles;
}
By default
fetch is
lazy...ok, I am fine with that.
Now in my hibernate repository I have the follow
Code:
@Override
public Articulo getArticuloCompleteById(Integer id){
return (Articulo) sessionFactory.getCurrentSession().get(Articulo.class, id);
}
And work fine....
but for some cases (other use case or business logic) I am receiving the Lazy exception....
Therefore I must know work in this way
Code:
@Override
public Articulo getArticuloCompleteById(Integer id){
//return (Articulo) sessionFactory.getCurrentSession().get(Articulo.class, id);
Articulo articulo = (Articulo) sessionFactory.getCurrentSession().get(Articulo.class, id);
Hibernate.initialize(articulo.getComprobanteDetalles());
return articulo;
}
Practically the new line is
Hibernate.initialize(articulo.getComprobanteDetalles());My question is...since the method
getArticuloCompleteById(Integer id) can be used from many
use cases or business logic, practically I dont know or cant generalize when the
Hibernate.initialize() should be used or not
Even worst, consider the follow scenario.
Code:
@Override
public Articulo getArticuloCompleteById(Integer id){
//return (Articulo) sessionFactory.getCurrentSession().get(Articulo.class, id);
Articulo articulo = (Articulo) sessionFactory.getCurrentSession().get(Articulo.class, id);
Hibernate.initialize(otherEntityInTheRelation01);
Hibernate.initialize(otherEntityInTheRelation02);
Hibernate.initialize(otherEntityInTheRelation03);
Hibernate.initialize(someCollectionofEntities01);
Hibernate.initialize(someCollectionofEntities02);
Hibernate.initialize(someCollectionofEntities03);
return articulo;
}
1) one
use case only require the
Articulo initialized ... but the rest are initiliazed unnecessary too...2) other
use case would only require
Articulo and
otherEntityInTheRelation01,otherEntityInTheRelation02 initialized... but the rest are initiliazed unnecessary too...3) other
use case would only require
Articulo and
otherEntityInTheRelation03,someCollectionofEntities01 initialized... but the rest are initiliazed unnecessary too...Therefore
1) If I apply for each relation of entities and collection the Hibernate.initialize()
Am I doing a negative impact in the performance?
2) If I apply for each relation of entities and collection the Hibernate.initialize()
Am I ignoring the lazy approach and using/applying the eager approach now instead?
Thanks in advance for your support.