Given the following classes/mappings:

    public class Person
    {
        public virtual Guid Id { get; protected set; }
        private readonly ISet<Pet> _pets = new HashedSet<Pet>();
        public virtual ISet<Pet> Pets
        {
            get { return _pets; }
        }
    }

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
namespace="ConsoleApplication1" assembly="ConsoleApplication1">
  <class name="Person">
    <id name="Id">
      <generator class="guid.comb" />
    </id>
    <set name="Pets" cascade="all" access="nosetter.camelcase-underscore" >
      <key column="OwnerId" />
      <one-to-many class="Pet" />
    </set>     
  </class>
</hibernate-mapping>


    public class Pet
    {
        public virtual Guid Id { get; protected set; }
        public virtual string Name { get; set; }
    }

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
namespace="ConsoleApplication1" assembly="ConsoleApplication1">
  <class name="Pet">
    <id name="Id">
      <generator class="guid.comb" />
    </id>
    <property name="Name"/>
  </class>
</hibernate-mapping>


and the following code:

ISessionFactory factory = configuration.BuildSessionFactory();
           
            using(var session = factory.OpenSession())
            using(var transaction = session.BeginTransaction())
            {
                var person = new Person();
                person.Pets.Add(new Pet {Name = "Rex"});
                session.Save(person);
                transaction.Commit();
            }

Three queries get issued to the DB:

INSERT INTO Person (HasPets, Id) VALUES (@p0, @p1);@p0 = True, @p1 = 
9e3f39b8-616a-44b2-8841-9c060166e810

INSERT INTO Pet (Name, Id) VALUES (@p0, @p1);@p0 = 'Rex', @p1 = 
1191cba6-d1f4-4634-9403-9c060166e83e

UPDATE Pet SET OwnerId = @p0 WHERE Id = @p1;@p0 = 
9e3f39b8-616a-44b2-8841-9c060166e810, @p1 = 
1191cba6-d1f4-4634-9403-9c060166e83e


Now, isn't the UPDATE superfluous? Why can't/won't NHibernate set the 
OwnerId, in the 2nd INSERT. It does know the Id of the person, because 
it did generate it itself, so what's the deal? What am I missing?

Krzysztof

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to