-->
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.  [ 5 posts ] 
Author Message
 Post subject: hibernate createCriteria issue
PostPosted: Wed Apr 12, 2006 10:43 pm 
Newbie

Joined: Wed Jan 25, 2006 7:16 pm
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.1

Mapping documents:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="com.haysmed.jobapplication.hibernate.JobApplication" table="JOB_APPLICATION" schema="HMCPORTAL">
<id name="applicationId" type="java.lang.Long">
<column name="APPLICATION_ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">job_application_id</param>
</generator>
</id>

....

<set name="jobApplicationComments" inverse="true" order-by="CREATED_DATE" >
<key>
<column name="APPLICATION_ID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="com.haysmed.jobapplication.hibernate.JobApplicationComments" />
</set>
<set name="jobCategorizes" inverse="true">
<key>
<column name="APPLICATION_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.haysmed.jobapplication.hibernate.JobCategorize" />
</set>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():

try {
/* get session of the current thread */
factory = (SessionFactory) servlet.getServletContext().getAttribute(HibernatePlugin.KEY_NAME);

session = factory.openSession();
tx = session.beginTransaction();
logger.debug("Getting all job applications by name.");
Criteria crit = session.createCriteria(JobApplication.class)
.setFetchMode("jobApplicationComments", FetchMode.JOIN)
.add(Expression.or(Expression.like("firstName", "%"+name+"%").ignoreCase(),Expression.like("lastName", "%"+name+"%").ignoreCase()))
.addOrder(Order.desc("createdDate"));

apps = crit.list();
tx.commit();
} catch(Exception e) {
logger.error(e.toString());
} finally {
session.close();
}

Full stack trace of any exception that occurs:

NA

Name and version of the database you are using:

Oracle 10.2.0.1.0

The generated SQL (show_sql=true):

Hibernate: select this_.APPLICATION_ID as APPLICAT1_1_, this_.ACTION_AGAINST_LICENSE as ACTION2_0_1_, this_.ACTION_AGAINST_LICENSE_EXPLAIN as ACTION3_0_1_,

.....

from HMCPORTAL.JOB_APPLICATION this_ left outer join HMCPORTAL.JOB_APPLICATION_COMMENTS jobapplica2_ on this_.APPLICATION_ID=jobapplica2_.APPLICATION_ID where (lower(this_.FIRST_NAME) like ? or lower(this_.LAST_NAME) like ?) order by jobapplica2_.CREATED_DATE, this_.CREATED_DATE desc

Debug level Hibernate log excerpt:



The problem is that when i set the FetchMode the results are incorrect. I'm getting an application for each comments. So if and application has 5 comments, there are 5 entries in the list. It's an outer join, I need this to be an inner join.

If I remove the setFetchMode I receive the failed to lazily initialize a collection for the comments object.

So what am I missing????


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 13, 2006 10:37 am 
Senior
Senior

Joined: Wed Aug 17, 2005 12:56 pm
Posts: 136
Location: Erie, PA (USA)
Try using createAlias() rather than .setFetchMode()

Curtis ...

_________________
---- Don't forget to rate! ----


Top
 Profile  
 
 Post subject: update...
PostPosted: Thu Apr 13, 2006 12:08 pm 
Newbie

Joined: Wed Jan 25, 2006 7:16 pm
Posts: 5
Changed to....

Criteria crit = session.createCriteria(JobApplication.class)
//.setFetchMode("jobApplicationComments", FetchMode.JOIN)
.createAlias("jobApplicationComments","comments")
.add(Expression.or(Expression.like("firstName", "%"+name+"%").ignoreCase(),Expression.like("lastName", "%"+name+"%").ignoreCase()))
.addOrder(Order.desc("createdDate"));


Getting the following error....

ERROR org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.haysmed.jobapplication.hibernate.JobApplication.jobApplicationComments - no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.haysmed.jobapplication.hibernate.JobApplication.jobApplicationComments - no session or session was closed


Top
 Profile  
 
 Post subject: one solution
PostPosted: Tue Apr 18, 2006 12:45 pm 
Newbie

Joined: Wed Jan 25, 2006 7:16 pm
Posts: 5
One solution would be to loop through the list and get the comments while the session is still open. It seems like there wold be a better way to do this.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 26, 2006 12:49 am 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Did you tried playing with lazy attribute in the mapping file as shown below

Code:
<set name="jobApplicationComments"
        inverse="true"
        lazy="true"
        order-by="CREATED_DATE" >
<key>
<column name="APPLICATION_ID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many
class="com.haysmed.jobapplication.hibernate.JobApplicationComments" />
</set>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.