-->
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.  [ 4 posts ] 
Author Message
 Post subject: lazy = true not working
PostPosted: Wed Mar 02, 2005 6:07 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
I have a driver class called GrouptTest (See listings section below). I am trying to explore the lazy loading feature in hibernate. I have a group object that has a collection of stories.

I have one Hibernate session created for the Driver class by making a call below:
Code:
....
HibernateSession.openSession();
....
....
// My dao objects use the same session for the execution of the test class
....
....
HibernateSession.closeSession();
....


I get a Group object by name, by doing:
Code:
...
Group group = groupDAO.findGroup("accounting", false);
...


I then get a List of stories by making a call to:
Code:
...
List stories = group.getStories();
if(stories != null)
...


THE PROBLEM is that I am facing is that when i make a call to findGroup (See code below), my group object is returned with the story objects populated in the collection inside it.?????? (NOTE: This happens before the call to group.getStories()). Why does this happen even when I specify lazy="true" in my Group.hbm.xml file?????

Listngs:

Code:
public class GroupTest
{
    public static void main(String [] args) throws HibernateException
    {
        HibernateSession.openSession();
       
        DAOFactory daoFactory = DAOFactory.getDAOFactory(DAOFactory.DS);
             
        InitializeDAO initDAO = daoFactory.getInitializeDAO();
        boolean flag  = initDAO.initializeDS();
       
        if(flag == false)
        {
            System.out.println("Could not initialize data source");
            System.exit(0);
        }
       
        GroupDAO groupDAO = daoFactory.getGroupDAO();
        Group sp = new Group("accounting");
               
        ArrayList list = new ArrayList();
        for(int i = 0; i < 1000; i++)
        {
            list.add(new Story("Story Number: " + i));   
        }
        sp.setStories(list);
       
        groupDAO.insertGroup(sp);
       
        Group group = groupDAO.findGroup("accounting");
       
        if(group != null)
        {
            System.out.println("Group name of the retrieved group is: " + group.getName());
           
            List stories = group.getStories();
            if(stories != null)
            {
               Iterator iter = stories.iterator();
              
               while(iter.hasNext())
               {
                   Story s = (Story)iter.next();
                   System.out.println("\tStory: " + s.getInfo());
               }
            }
        }
        else
            System.out.println("There is no Group present by that name");   
       
        HibernateSession.closeSession();
    }
}


Code:
...
...
<hibernate-mapping>
    <class name="med.allegro.sp.persistent.Group" table="grouptable" discriminator-value="parent">
        <id name="id" unsaved-value="0">
                <generator class="native"/>
        </id>
       
        <discriminator column="type"/>
       
        <list name="stories" cascade="all" lazy="true">
           <key column="parent_id"/>
           <index column="idx"/>
           <one-to-many class="med.allegro.sp.persistent.Story"/>
        </list>
...
...


Code:
    public Group findGroup(String name)
    {
        Group group = null;
       
        try
        {
            Session session = HibernateSession.currentSession();           
            Query q = session.createQuery("from med.allegro.sp.persistent.Group as groups where groups.name=:gname");
            q.setString("gname", name);         
            group = (Group)q.list().get(0);
        }
        catch(Exception e)
        {
               e.printStackTrace();
        }
       
        return group;
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 6:24 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
It looks like you are creating a group, setting the stories, then loading it again. Since this is in the same session, the group is in the session cache and doesn't go to the database. It also means the stories you added are still attached, as they are also in the session memory.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 7:21 pm 
Beginner
Beginner

Joined: Sun Feb 20, 2005 12:14 am
Posts: 49
I have this in my hbm.cfg.xml file:

Code:
        <property name="hibernate.cache.provider_class">
             org.hibernate.cache.EhCacheProvider
        </property>


Except the above specified in my cfg.xml file I have no caching related api calls or declarations in the code. Does hibernate does some fair amount of caching on its own within a session? Is there a article or documentation I can read up on related to Hibernate's default caching mechanisms?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 02, 2005 7:30 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
It looks as though your have configured a second-level cache as well, which may also return cached objects.

A Hibernate Session keeps track of the graph of objects being modified, and retrieves/flushes to the database only when needed. It therefore does not (thankfully) go to the database with every call. This makes Hibernate very efficient.
http://www.hibernate.org/hib_docs/v3/re ... ssioncache

Both the first and second level caches are transparent to the user code, beyond being configured.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.