Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hibernate createCriteria issue

 
alan wamser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two tables mapped using hibernate (jobApplication, jobApplicationComments).

The jobApplication object has the following details...

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

with the following in the class...

private Set jobApplicationComments = new HashSet(0);


In my business logic I search for all applications using the following.

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"));

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. Looks like an outer join.

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

So what am I missing???
 
alan wamser
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
still having this issue...
 
Thomas Fischer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alan,

i think it is normal in hibernate to get duplicate entries in the resultlist when using FetchMode.JOIN.

You could get rid of the duplicate entries by doing something like this:



Another way - which i didn't try myself - would be:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic