-->
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.  [ 2 posts ] 
Author Message
 Post subject: problems with list mapping
PostPosted: Sat Feb 20, 2010 1:30 pm 
Newbie

Joined: Thu May 21, 2009 4:07 pm
Posts: 4
hi,
i know i'm missing something blindingly obvious, but i'm struggling to get the list mapping to work. say i have:
Code:
   public class ClassA
   {
      public ClassA()
      {
         TheBs = new List<ClassB>();
      }

      public virtual int Id { get; set; }
      public virtual string Prop1 { get; set; }
      public virtual IList<ClassB> TheBs { get; protected set; }
   }

   public class ClassB
   {
      public virtual int Id { get; set; }
      public virtual string Prop2 { get; set; }
   }


and i map it with:
Code:
   <class name="ClassA">
      <id name="Id">
         <generator class="native" />
      </id>

      <property name="Prop1"/>
      <list name="TheBs" cascade="all-delete-orphan" inverse="false" >
         <key column="AID"/>
         <index column="BIDX"/>
         <one-to-many class="ClassB"/>         
      </list>
   </class>

   <class name="ClassB">
      <id name="Id">
         <generator class="native" />
      </id>

      <property name="Prop2"/>
   </class>


if i run the following unit test:
Code:
      [Test]
      public void Should_return_a_with_one_b_child()
      {
         ClassA a = new ClassA { Prop1 = "prop1" };
         a.TheBs.Add(new ClassB {  Prop2 = "prop2" });

         session.Save(a);
         int id = a.Id;
         session.Evict(a);

         a = session.Get<ClassA>(id);

         Assert.That(a, Has.Property("Prop1").EqualTo("prop1"));
         Assert.That(a.TheBs, Has.Count.EqualTo(1));
      }


i get:
Code:
NHibernate: INSERT INTO applicant.ClassA (Prop1) VALUES (@p0); select SCOPE_IDENTITY(); @p0 = 'prop1'
NHibernate: INSERT INTO applicant.ClassB (Prop2) VALUES (@p0); select SCOPE_IDENTITY(); @p0 = 'prop2'
NHibernate: SELECT classa0_.Id as Id6_0_, classa0_.Prop1 as Prop2_6_0_ FROM applicant.ClassA classa0_ WHERE classa0_.Id=@p0; @p0 = '2'
NHibernate: SELECT thebs0_.AID as AID__1_, thebs0_.Id as Id1_, thebs0_.BIDX as BIDX__1_, thebs0_.Id as Id7_0_, thebs0_.Prop2 as Prop2_7_0_ FROM applicant.ClassB thebs0_ WHERE thebs0_.AID=@p0; @p0 = '2'


how do i get class b's to cascade correctly. what's wrong with my mapping? i've googled this as much as possible, but all the examples use bags. maybe there's a reason for this...
ps. i'm on version 1.2.


Top
 Profile  
 
 Post subject: Re: problems with list mapping
PostPosted: Tue Feb 23, 2010 3:37 am 
Newbie

Joined: Thu May 21, 2009 4:07 pm
Posts: 4
well, i puzzled it out. a session.Flush() after the sesson.Save makes all the difference!


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