-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Hibernate.initialize(...) has impact on performance?
PostPosted: Thu May 02, 2013 10:36 pm 
Beginner
Beginner

Joined: Fri Jun 23, 2006 6:40 pm
Posts: 25
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.

_________________
kill your pride, share your knowledge with all


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.