views:

9923

answers:

7

Relating to my earlier question, I want to ensure all the child objects are loaded as I have a multiple threads that may need to access the data (and thus avoid lazy loading exceptions). I understand the way to do this is to use the "fetch" keyword in the query (EJB QL). Like this:

select distinct o from Order o left join fetch o.orderLines

Assuming a model with an Order class which has a set of OrderLines in it.

My question is that the "distinct" keyword seems to be needed as otherwise I seem to get back an Order for each OrderLine. Am I doing the right thing?

Perhaps more importantly, is there a way to pull in all child objects, no matter how deep? We have around 10-15 classes and for the server we will need everything loaded... I was avoiding using FetchType.EAGER as that meant its always eager and in particular the web front end loads everything - but perhaps that is the way to go - is that what you do? I seem to remember us trying this before and then getting really slow webpages - but perhaps that means we should be using a second-level cache?

+2  A: 

I'm not sure about using the fetch keyword in your EJBQL, you might be getting it confused with the annotation...

Have you tried adding the FetchType property to your relationship attribute?

@OneToMany(fetch=FetchType.EAGER)?

See:

http://java.sun.com/javaee/5/docs/api/javax/persistence/FetchType.html http://www.jroller.com/eyallupu/entry/hibernate_exception_simultaneously_fetch_multiple

Jeremy
yes, that is the correct answer
bosnic
A: 

That would only work for ManyToOne relations and for them @ManyToOne(fetch=FetchType.EAGER) would probably appropriate.

Fetching more than one OneToMany relation eagerly is discouraged and/or does not work as you can read in the link Jeremy posted. Just think about the SQL statement that would be needed to do such a fetch...

jrudolph
+2  A: 

Changing the annotation is a bad idea IMO. As it can't be changed to lazy at runtime. Better to make everything lazy, and fetch as needed.

I'm not sure I understand your problem without mappings. Left join fetch should be all you need for the use case you describe. Of course you'll get back an order for every orderline if orderline has an order as its parent.

James Law
A: 

What I have done is to refactor the code to keep a map of objects to entity managers and each time I need to refresh, close the old entitymanager for the object and open a new one. I used the above query without the fetch as that is going too deep for my needs - just doing a plain join pulls in the OrderLines - the fetch makes it go even deeper.

There are only a few objects that I need this for, around 20, so I think the resource overhead in having 20 open entitymanagers is not an issue - although the DBAs may have a different view when this goes live...

I also re-worked things so that the db work is on the main thread and has the entity manager.

Chris

Chris Kimpton
A: 

If the problem is just LazyInitializationExceptions, you can avoid that by adding an OpenSessionInViewFilter.
This will allow the objects to be loaded in the view, but will not help with the speed issue.

     <filter>
     <filter-name>hibernateFilter</filter-name>
     <filter-class> org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
     </filter-class>
    </filter>
    <filter-mapping>
     <filter-name>hibernateFilter</filter-name>
     <url-pattern>/*</url-pattern>
    </filter-mapping>
ncgz
Thanks, but this answer relates to webapps/servlets and not a standalone app, which is what I am working with.
Chris Kimpton
+1  A: 

You might be able to do something like that using a (detached) criteria query, and setting the fetch mode. E.g.,

Session s = ((HibernateEntityManager) em).getSession().getSessionFactory().openSession();
DetachedCriteria dc = DetachedCriteria.forClass(MyEntity.class).add(Expression.idEq(id));
dc.setFetchMode("innerTable", FetchMode.JOIN);
Criteria c = dc.getExecutableCriteria(s);
MyEntity a = (MyEntity)c.uniqueResult();
Mike Desjardins
Thanks, but the problem is that I want to use the persisted objects outside the main, EM owning thread, to get at transient data about the current state of things. So I cannot use the main EM but also want the same objects :(
Chris Kimpton
A: 

Have you tried using a result transformer? If you use Criteria queries, you can apply a result transformer (although there are some problems with pagination and result transformer):

Criteria c = ((Session)em.getDelegate()).createCriteria(Order.class);
c.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
c.list();

the em.getDelegate() is a hack that only works if you are using hibernate.

Perhaps more importantly, is there a way to pull in all child objects, no matter how deep? We have around 10-15 classes and for the server we will need everything loaded... I was avoiding using FetchType.EAGER as that meant its always eager and in particular the web front end loads everything - but perhaps that is the way to go - is that what you do? I seem to remember us trying this before and then getting really slow webpages - but perhaps that means we should be using a second-level cache?

If you are still interested, I responded a similar question in this thread how to serialize hibernate collections.

Basically you use a utility called dozer that maps beans onto another beans, and by doing this you trigger all your lazy loads. As you can imagine, this works better if all collections are eagerly fetched.

Miguel Ping