I have a class Journal
, which has an IList
of JournalLine
objects.
I created a Journal
and accidentally ran it through the same line generation method twice. The start of this method calls _journalLines.Clear()
and the end does _session.SaveOrUpdate(journal)
. So I have this sequence:
- Generate
Journal
with no lines, callSaveOrUpdate
. That's fine. - Generate three
JournalLines
with IDs 1,2,3, add toJournal._journalLines
and callSaveOrUpdate
- Call
Journal._journalLines.Clear()
. A breakpoint after this shows the lines list to be empty. - Generate three
JournalLines
with IDs 4,5,6, add toJournal._journalLines
and callSaveOrUpdate
. A breakpoint here shows_journalLines
to have three things in it. - I'm left with a
Journal
that has 6 lines.
This all takes place in one transaction and nothing is persisted in the database until it's finished.
Why is this merging the two collections? It seems to me that it should get to the last SaveOrUpdate
where the breakpoint shows it's got three lines, and persist it as having three lines. Are the other three hanging around in memory somewhere because there hasn't been a flush yet?
Edit: Mapping
public JournalMap()
{
// Other stuff
HasMany(x => x.JournalLines)
.Access.CamelCaseField(Prefix.Underscore)
.Cascade.AllDeleteOrphan();
}
public JournalLineMap()
{
// Other stuff
References(x => x.Journal);
}
Journal
has these:
private readonly IList<JournalLine> _journalLines = new List<JournalLine>();
public virtual IEnumerable<JournalLine> JournalLines
{ get { return _journalLines; } }
The actual code that generates the lines is way too complicated to add here, but it calls _journalLines.Add(journalLine);
after generating them, and then calls this, T being a Journal
public T Add(T entity)
{
_session.SaveOrUpdate(entity);
return entity;
}
Before ultimately calling _session.Flush()
and _session.Transaction.Commit();
if the flush doesn't error.