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!!