Consider the following scenario: the user has a set of spices, and each spice can be “in stock” or “run out” (indicated by the RunOut logical property).
I matched this relationship with FluentNHibernate using such a component
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 Spice collection in the above display belongs to the UserSpice class (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 (for example, changing spice to RunOut = true), all custom spices are removed and reinserted.
I understand that this is because NHibernate does not have the ability to uniquely identify which sps-spice link it should update.
How can I simulate this differently to avoid this delete-and-reinsert behavior?
source share