Hi guys. How to force NHibernate not to generate UPDATE for each element in collection if I change only one element of that collection. Here it is:
internal class Master
{
private int _id;
public string Name { get; set; }
public ISet<Slave> Slaves { get; set; }
public Master()
{
Slaves = new HashedSet<Slave>();
}
}
internal class Slave
{
private int _id;
public string Name { get; set; }
public Master Master { get; set; }
}
Mappings:
<class name="nHibernateTutorials.Slave, nHibernateTutorials" lazy="false">
<id access="field" name="_id" column="id">
<generator class="native"/>
</id>
<property access="property" name="Name" column="name"/>
<many-to-one access="property" name="Master" column="master"/>
</class>
<class name="nHibernateTutorials.Master, nHibernateTutorials" lazy="false">
<id access="field" name="_id" column="id">
<generator class="native"/>
</id>
<property access="property" name="Name" column="name"/>
<set access="property" name="Slaves" cascade="save-update">
<key column="master"/>
<one-to-many class="nHibernateTutorials.Slave, nHibernateTutorials"/>
</set>
</class>
Code that updates collection element:
Master m = new Master {Name = "Kenny"};
Slave s1 = new Slave { Name = "Cartman", Master = m};
m.Slaves.Add(s1);
Slave s2 = new Slave {Name = "Kyle", Master = m};
m.Slaves.Add(s2);
Slave s3 = new Slave {Name = "Stan", Master = m};
m.Slaves.Add(s3);
DbManager.SaveObject(m);
s1.Name = "Daisy";
DbManager.SaveObject(m);
Code in DbManager.SaveObject simply opens new session and uses SaveOrUpdate to update object.
If I change one of the elements from the Slaves collection of master and then try to update master, NHibernate generates SQL for updating all elements in Slaves collection. But I need to update only one element.
Thank you.