Consider the following scenario: A user has a collection of spices, and each spice can either be 'in stock' or 'run out' (denoted by a boolean RunOut property).
I have mapped this relationship with FluentNHibernate using a component like this
public UserMappings()
{
Id(x => x.Id);
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.Email).Length(255).Unique();
HasOne(x => x.Credentials).Cascade.SaveUpdate().Fetch.Select();
HasMany(x => x.Spices)
.Component(c =>
{
c.Map(x => x.RunOut);
c.References(x => x.BaseSpice).Fetch.Join();
}).Table("User_Spices").Fetch.Join();
}
The "Spices" collection in the above mapping is to UserSpice class (a value-object):
public class UserSpice
{
public virtual Spice BaseSpice { get; protected set; }
public virtual bool RunOut { get; protected set; }
public static UserSpice Create(Spice baseSpice, bool runOut)
{
var userSpice = new UserSpice {BaseSpice = baseSpice, RunOut = runOut};
return userSpice;
}
}
This works 'fine' - however when I update any of the components (change a spice to RunOut = true, for example), all the user's spices are deleted and re-inserted.
I understand that this is happening because NHibernate has no way of uniquely identifying which user-spice reference it should update.
How can I model this differently to avoid this delete-and-reinsert behaviour?