-->
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.  [ 6 posts ] 
Author Message
 Post subject: Many to Many question
PostPosted: Sun Dec 11, 2005 5:49 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
Again I'm pretty sure that I'm missing the obvious, but I can't make it work:

User has many Blogs
Blogs has many Users

I manage to save just fine, and cascades works, the problem is that NHibernate isn't saving to the association table.

The user class mapping:

Code:
<class
      lazy='true'
        name='NHibernate.Generics.Tests.User, NHibernate.Generics.Tests'
        table='Users'>
    <id
       column='user_id'
          name='UserId'>
      <generator
             class='native' />
    </id>
    <property
       column='user_name'
          name='UserName'
          type='string' />
    <set
          name='Blogs'
       cascade='save-update'
       lazy='true'
       inverse='false'
       access='NHibernate.Generics.GenericAccessor, NHibernate.Generics'
          table='UsersBlogs'>
      <key
             column='user_id' />
      <many-to-many
             class='NHibernate.Generics.Tests.Blog, NHibernate.Generics.Tests'
             column='blog_id' />
    </set>
  </class>


The Blog class mapping:

Code:
<class
     lazy='true'
       name='NHibernate.Generics.Tests.Blog, NHibernate.Generics.Tests'
       table='Blogs'>
      <id
          name='BlogID'
          column='blog_id'
          unsaved-value='0'>
         <generator
             class='native' />
      </id>
      <property
          name='BlogName'
          column='blog_name' />
      <set
          name='Posts'
          access='NHibernate.Generics.GenericAccessor, NHibernate.Generics'
          lazy='true'
       cascade='all-delete-orphan'
          inverse='true'>
         <key
             column='post_blogid' />
         <one-to-many
             class='NHibernate.Generics.Tests.Post, NHibernate.Generics.Tests' />
      </set>
    <set
      name='Users'
      access='NHibernate.Generics.GenericAccessor, NHibernate.Generics'
      cascade='save-update'
      inverse='true'
      table='UsersBlogs'>
      <key
             column='blog_id' />
      <many-to-many
             class='NHibernate.Generics.Tests.User, NHibernate.Generics.Tests'
             column='user_id' />
    </set>
   </class>



The code that fails:
Code:
session.Save(user1);
session.Save(user2);
//blog1 & blog2 are saved by cascades
session.Dispose();
seesion = factory.OpenSession();
User newUser = (User)session.Load(typeof(User), user1.Id);

Assert.AreEqual(2, user1.Blogs.Count);


The code cause the following to happen:

-User1 saved
-User2 saved

-Blog1 saved
-Blog2 saved

No association is saved :-(


Top
 Profile  
 
 Post subject: Many to Many question
PostPosted: Sun Dec 11, 2005 10:00 pm 
Newbie

Joined: Wed Dec 07, 2005 12:32 am
Posts: 11
Location: Chennai, India
Hi Ayende Rahien,

Some pointers:

    o Are you using uni-directional or bi-directional mapping between Posts and Blogs?
    o Are you setting the values for Posts?
    o Are you setting the parent for Posts?


HTH,
~Venki


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 7:46 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
I think you misunderstood me.

The problem is Users & Blogs.

The Users & Blogs rows are created property.

The problem is that the connection between them (a row on UsersBlogs) is not being created, and I'm not sure why.

Yes, the connection is bidi, and it's working in memory.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 9:06 am 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
I don't see any call to Flush or Commit in your code, are you calling it?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 12:36 pm 
Contributor
Contributor

Joined: Sat Sep 24, 2005 11:25 am
Posts: 198
No, I assumed that Dispose would handle that.
Does it matter?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 13, 2005 1:11 pm 
Contributor
Contributor

Joined: Wed May 11, 2005 4:59 pm
Posts: 1766
Location: Prague, Czech Republic
Yes, of course. You have to call Commit (or Flush) explicitly


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.