-->
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.  [ 4 posts ] 
Author Message
 Post subject: Criteria, Projections and Lazy Loading
PostPosted: Tue Mar 23, 2010 10:01 am 
Newbie

Joined: Tue Mar 23, 2010 9:40 am
Posts: 2
I am hoping the experts can help me out with this problem. Due to some recent performance issues with our application we are changing some optional data from EAGER to LAZY loading and have had some mixed results. There is one particular scenario that I have not been able to solve.

The following code consistently throws the dreaded LazyInitializationException when I try to access any of the fields in the returned ArrayList after the session has been closed.
Code:
Criteria criteria = session.createCriteria(MostRecentTroubleTicketDetail.class);
criteria.setProjection(Projections.property("troubleTicketDetail");
criteria.setFetchMode("troubleTicketDetail", FetchMode.JOIN);

ArrayList<MostRecentTroubleTicketDetail> ttds = (ArrayList<MostRecentTroubleTicketDetail>) criteria.list();

session.getTransaction().commit();
session.close();


MostRecentTroubleTicketDetail.java (I have omitted the irrelevant data but there are a lot of other fields mainly used for querying purposes)
Code:
@Entity
@Table(name = "MOST_RECENT_TTDS_VIEW")
public class MostRecentTroubleTicketDetail {
    /**
     *
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TROUBLE_TICKET_DETAIL_ID")
    private TroubleTicketDetail troubleTicketDetail;
}


Any help or suggestions on how to properly write the Criteria query are greatly appreciated.

Thanks!!


Top
 Profile  
 
 Post subject: Re: Criteria, Projections and Lazy Loading
PostPosted: Wed Mar 24, 2010 4:19 am 
Regular
Regular

Joined: Thu Dec 10, 2009 10:53 am
Posts: 50
You may try to run Hibernate.initialize() on the elements of the list before closing the session, because hibernate probably returned you a proxy and not the real object.

How do you know hibernate uses an arraylist? I haven't found that in the source code. You should probably use List<>.

Shouldn't the type of the list be TroubleTicketDetail instead of MostRecentTroubleTicketDetail, because troubleTicketDetail is of type TroubleTicketDetail ?


Top
 Profile  
 
 Post subject: Re: Criteria, Projections and Lazy Loading
PostPosted: Wed Mar 24, 2010 8:49 am 
Newbie

Joined: Tue Mar 23, 2010 9:40 am
Posts: 2
Quote:
You may try to run Hibernate.initialize() on the elements of the list before closing the session, because hibernate probably returned you a proxy and not the real object.

Thanks kossmo, that will definitely work. If you wanted to fetch a bunch of fields using the Hibernate.initialize() could get cumbersome. Is there a cleaner way using the criteria.setFetchMode()?

Quote:
Shouldn't the type of the list be TroubleTicketDetail instead of MostRecentTroubleTicketDetail, because troubleTicketDetail is of type TroubleTicketDetail ?

Yep. Sorry for the typo.


Top
 Profile  
 
 Post subject: Re: Criteria, Projections and Lazy Loading
PostPosted: Fri May 07, 2010 5:35 am 
Newbie

Joined: Fri May 07, 2010 5:33 am
Posts: 1
I have the same problem... use Hibernate.initialize() on each item is so cumbersome


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

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.