I *don't* want to have bidirectional ref. I want my model just like it is now. And still, it's besides the point which is - why is NHibernate doing two rountrips to the DB for something it could (or could it?) do in one.
Krzysztof Roger Kratz pisze: > Have a bidirectional ref and mark the collection with inverse=true. > > ________________________________________ > Från: [email protected] [[email protected]] för Krzysztof > Koźmic [[email protected]] > Skickat: den 11 maj 2009 21:52 > Till: [email protected] > Ämne: [nhusers] Superfluous update? > > 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 -~----------~----~----~----~------~----~------~--~---
