-->
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.  [ 5 posts ] 
Author Message
 Post subject: Problem with bidirectional navigation
PostPosted: Wed Jul 13, 2005 9:01 am 
Newbie

Joined: Wed Jul 13, 2005 8:03 am
Posts: 5
Hibernate version: 2.1.6
Database: MySql

Simple parent-and-children relationship. One category can have several subcategories. For example, Category "Computers" has two subcategories named "Monitors" and "DesktopPCs".

Code:
           
                      -------------
                      | Computers |
                      -------------
                             |
              ----------------------------
              |                           |
       -------------             --------------
       | Monitors |              | DesktopPCs |
       -------------             --------------


When I fetch "Computers" from database and list its subcategories, it works well, but if I navigate from "Monitors" to "Computers" then Nagivate back, it doesn't. Is it a bug or something.
Code:
private static void retrieve() {       
        Category root = null;
        String name = "Monitors";
       
       
        Session sess = HibernateUtil.getSession();
        try {
            HibernateUtil.beginTransaction();     
            Query q = sess.createQuery("from Category c where c.name = :name");
            q.setString("name", name);
            root = (Category)q.list().get(0);           
           
            //Navigate to "Computers"
            System.out.println(root.getParentCategory().getName());           

            //Navigate back to every child, fail
            Iterator iter = root.getParentCategory().getChildCategories().iterator();
            while(iter.hasNext())
                System.out.println(((Category)iter.next()).getName());
                       
           
            HibernateUtil.commitTransaction();
        } catch(InfrastructureException e) {
            HibernateUtil.rollbackTransaction();
        } catch(HibernateException e) {
            HibernateUtil.rollbackTransaction();           
        } finally {
            HibernateUtil.closeSession();
        }
    }


Part of output in console:
Quote:
Hibernate: select category0_.CATEGORY_ID as CATEGORY1_, category0_.NAME as NAME, category0_.PARENT_CATEGORY_ID as PARENT_C3_ from OA_CATEGORY category0_ where (category0_.NAME=? )
Hibernate: select category0_.CATEGORY_ID as CATEGORY1_0_, category0_.NAME as NAME0_, category0_.PARENT_CATEGORY_ID as PARENT_C3_0_ from OA_CATEGORY category0_ where category0_.CATEGORY_ID=?
Computers


Category.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping
>
    <class
        name="model.Category"
      lazy="true"
    >

        <id
            name="id"
            column="CATEGORY_ID"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

            <key
                column="fk_Category"
            >
            </key>

            <many-to-many
                class="model.Item"
                column="fk_Item"
                outer-join="auto"
             />

        </set>

        <set
            name="childCategories"
            lazy="true"         
            inverse="true"
            cascade="save-update"
            batch-size="10"
            sort="unsorted"
        >

            <key
                column="PARENT_CATEGORY_ID"
            >
            </key>

            <one-to-many
                  class="model.Category"
            />

        </set>

        <many-to-one
            name="parentCategory"
            outer-join="false"
            class="model.Category"
            cascade="none"         
            update="true"
            insert="true"
            column="PARENT_CATEGORY_ID"
        />

    </class>

</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 12:34 am 
Newbie

Joined: Wed Jul 13, 2005 8:03 am
Posts: 5
if I left out lazy='true" in
Code:
<hibernate-mapping>
    <class  name="model.Category"
               lazy="true"
    >
.....

, all goes smoothly. Please help me out of this problem. Does lazy fetch conflict with bidirectional nagivation?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 8:02 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
http://www.hibernate.org/hib_docs/v3/re ... pojo-final

I think you have both class lazy (no-proxies), and lazy association fetching which the above explicitly prohibits.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 10:46 pm 
Newbie

Joined: Wed Jul 13, 2005 8:03 am
Posts: 5
Thanks to your help, I locate the error finally.
A central feature of Hibernate, proxies, depends upon all getters of POJO being public and non-final(setters may also be proxied, but frequently we make them private or friendly for maintenance of data integrity in mem). Whereas I declare one getter to be final where potential error lurks.

It's because Hibernate's proxies use inheritence rather than composition to hook user's invocation of proxied objects. In another way, proxies are created dynamically by subclassing POJO at runtime so that typecasting and instanceof work perfectly


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 14, 2005 11:04 pm 
Newbie

Joined: Wed Jul 13, 2005 8:03 am
Posts: 5
Whether or not I can explain the situation as follow?

Maybe in the first senario, to navigate from parent to all children is called immediate fetch rather than lazy fetch, so lazy loading and proxy mechanism is not triggered. So everything seems right.

But the second senario, I navigate to parent then navigate back, then lazy loading should take effect and potential error emerge.

In what situation, we can tell whether immediate or lazy fetch occurs.


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