Can someone explain this little riddle to me about how NHibernate handles building blocks.
I have classes that look like this:
public class Blog { public virtual int Id { get; private set; } public virtual ISet<Comment> Comments { get; set; } } public class Comment { public virtual string CommentText { get; set; } public virtual DateTime Date { get; set; } }
and mappings of this type:
<class name="Blog" table="blog"> <id name="Id" column="id" unsaved-value="0"> <generator class="hilo"/> </id> <set name="Comments" table="blog_comments"> <key column="blog_id" /> <composite-element class="Comment"> <property name="CommentText" column="comment" not-null="true" /> <property name="Date" column="date" not-null="true" /> </composite-element> </set> </class>
However, when I make a selection, for example:
using (ITransaction transaction = session.BeginTransaction()) { Blog blog = session.CreateCriteria(typeof(Blog)) .SetFetchMode("Comments", FetchMode.Eager) .Add(Expression.IdEq(2345)) .UniqueResult(); transaction.Commit(); }
NHibernate issues a join option to get a blog with posts, but then removes all comments and then inserts comments! Why is he doing this? If I do not use a transaction, it will ONLY execute the selection, not DELETE and INSERT, as I expected. What am I missing? I am using NHibernate 2.0
source share