-->
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.  [ 1 post ] 
Author Message
 Post subject: Lazy loading many-to-many
PostPosted: Tue Apr 26, 2005 5:58 am 
Newbie

Joined: Thu Mar 17, 2005 11:13 am
Posts: 7
Hi,

I have a session facade method like this:

Code:
    public Set getUserPublishers(Long userId) {
        try {
            User user = (User) HibernateUtil.getSession()
               .get(User.class, userId);
            if (user == null)
                throw new IllegalArgumentException("No such user");
            Hibernate.initialize(user.getPublishers());
            return user.getPublishers();
        } catch (HibernateException e) {
            throw new InfrastructureException(e);
        } finally {
            HibernateUtil.closeSession();
        }
    }



But when I try to access publisher fields in client side (outside session), I get LazyInitializationException. Why isn't Set initialized? Is there something wrong with my mappings?

If I change to <class name="Publisher" lazy="false">, this code works. But how can I make it work with lazy? (I thought "class lazy" only affects many-to-one associations?)


Hibernate version: 2.1.8

Mapping documents: (Unimportant properties removed)
Code:
    <class
        name="User"
        table="users"
        dynamic-update="false"
        dynamic-insert="false"
        lazy="true"
    >
        <set
            name="publishers"
            table="publishers_users_link"
            lazy="true"
            inverse="false"
            cascade="save-update"
            sort="unsorted"
        >
              <key
                  column="user_id"
              >
              </key>

              <many-to-many
                  class="Publisher"
                  column="publisher_id"
                  outer-join="auto"
               />
        </set>

    <class
        name="Publisher"
        table="publishers"
        dynamic-update="false"
        dynamic-insert="false"
        lazy="true"
    >
        <set
            name="users"
            table="publishers_users_link"
            lazy="true"
            inverse="true"
            cascade="save-update"
            sort="unsorted"
        >
              <key
                  column="publisher_id"
              >
              </key>

              <many-to-many
                  class="User"
                  column="user_id"
                  outer-join="auto"
               />
        </set>



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.