because categories uses lazy load
On Sun, Nov 18, 2012 at 5:20 AM, Curdin Caspar <[email protected]>wrote: > Hi > > I have a very strange problem. I implement for a small project some > unit-testing for the nhibernate queries. > The one-to-many is working correctly if I get a user which already exists > and has categories but not if I create a new user with new categories. > > Here's an extract from the mapping: > > ?xml version="1.0" encoding="utf-8" ?> > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > assembly="Homebank.Entities" namespace="Homebank.Entities"> > <class name="User" table="[User]"> > <id name="DataId" column="DataId"> > <generator class="identity" /> > </id> > <property name="Name" column="Name" not-null="true" /> > <bag name="Categories" inverse="true"> > <key column="User_DataId" /> > <one-to-many class="Category" /> > </bag> > </class> > </hibernate-mapping> > > <?xml version="1.0" encoding="utf-8" ?> > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > assembly="Homebank.Entities" namespace="Homebank.Entities"> > <class name="Category" table="Category"> > <id name="DataId" column="DataId"> > <generator class="identity" /> > </id> > <property name="Name" column="Name" not-null="true" /> > <many-to-one name="User" column="User_DataId" class="User" not-null="true" > /> > </class> > </hibernate-mapping> > > And here's the code: > > User user = new User() { Name = "UserForTestingCategoryCollection" }; > myUserManager.Save(user); > > Category c1 = new Category() { Name = "c1", User = user }; > Category c2 = new Category() { Name = "c2", User = user }; > Category c3 = new Category() { Name = "c3", User = user }; > myCategoryManager.Save(c1); > myCategoryManager.Save(c2); > myCategoryManager.Save(c3); > > User u = myUserManager.Get(user.DataId); > > Assert.AreEqual(3, u.Categories.Count); > > Those managers have the same share session which basically just opens a > transaction to save and retrieves the item by Session.Get<T>(id). > The user will have 0 categories. > > Has anyone an idea why this is not working as expected? > > -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To view this discussion on the web visit > https://groups.google.com/d/msg/nhusers/-/0lFtwSSox5oJ. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
