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