- This topic has 3 replies, 2 voices, and was last updated 18 years, 6 months ago by Haris Peco.
-
AuthorPosts
-
awamserMemberposted April 12, 2006 03:59 PM
——————————————————————————–
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????
Haris PecoMemberawamser ,
Try grab comments before closing session or send us complete mapping and I will try reproduce your case
Best
awamserMemberDoesn’t that seem like an extra step… I would have to loop through the list and get each comment… seems like there would be a better way to do this…
Haris PecoMemberIt’s hibernate lazy loading concepts – hibernate doesn’t load some raltion until access it
You have change lazy loading (in hibernate 3.0 default-lazy is true ) in mapping file with<hibernate-mapping default-lazy=”false”/>
but if you have disabled lazy loading and complete mappings (inverse set and many-to-one) you will case that loading some class load complete database in memory
If you have rich mappings then it is better that you use lazy loading and load in one session what you need
However, if you need all comments in every session then you can use default-lazy=”false” – there isn’t general way for all cases and you have to test your environment and mappingsThere are a lot threads about lazy loading on hibernate forums and there are good description in hibenate documentation
Best
-
AuthorPosts