facebook

hibernate createCriteria issue

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #250400 Reply

    awamser
    Member

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

    #250404 Reply

    Haris Peco
    Member

    awamser ,

    Try grab comments before closing session or send us complete mapping and I will try reproduce your case

    Best

    #250643 Reply

    awamser
    Member

    Doesn’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…

    #250652 Reply

    Haris Peco
    Member

    It’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 mappings

    There are a lot threads about lazy loading on hibernate forums and there are good description in hibenate documentation

    Best

Viewing 4 posts - 1 through 4 (of 4 total)
Reply To: hibernate createCriteria issue

You must be logged in to post in the forum log in