Update: Batch updates are now working, but as I access children bags are re-read from db even though the enitites have be loaded. Using NH Profiler to watch is awesome.
Unit Test:
NHibernateSession.Current.Transaction.Begin();
var reservationEventIds = new List<int>() {123, 124, 125};
var repository = new Repository<Reservation>();
var criteria =
repository.Session.CreateCriteria(typeof(Reservation))
.CreateAlias("ReservationEvents", "res")
.SetFetchMode("ReservationEvents", FetchMode.Eager)
.CreateAlias("res.Approvals", "app")
.SetFetchMode("res.Approvals", FetchMode.Eager)
.Add(new InExpression("res.Id", reservationEventIds.ToList().ConvertAll(x => (object)x).ToArray()))
.SetResultTransformer(new DistinctRootEntityResultTransformer());
var r = criteria.List<Reservation>().ToList();
r.ForEach(res => res.LastModifiedDate = DateTime.Now);
r.ForEach(res => res.ReservationEvents.ToList().ForEach(re => re.LastModifiedDate = DateTime.Now));
r.ForEach(
res =>
res.ReservationEvents.ToList().ForEach(
re => re.Approvals.ToList().ForEach(rea => rea.LastModifiedDate = DateTime.Now)));
NHibernateSession.Current.Transaction.Commit();
I am updating a handful of properties on each entity within a transaction. And batch size is set. I am seeing the ReservationEventApprovals batched, but each ReservationEvent starts a new batch. Thanks for any advice.
Like this:
{ ReservationEventApproval ReservationEventApproval }
ReservationEvent
{ ReservationEventApproval ReservationEventApproval }
ReservationEvent
Reservation
Here is a simple version of the maps:
<class name="Reservation" table="Reservation">
<id name="Id" column="ReservationId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property name="RequestorName" column="RequestorName" type="string" />
<property name="LastModifiedDate" column="LastModifiedDate" type="DateTime" />
<bag name="ReservationEvents" cascade="all-delete-orphan" inverse="true" lazy="true">
<key column="ReservationId" />
<one-to-many class="ReservationEvent"/>
</bag>
</class>
<class name="ReservationEvent" table="ReservationEvent">
<id name="Id" column="ReservationEventId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<property column="ReservationEventDate" type="DateTime" name="ReservationEventDate" />
<property column="LastModifiedDate" type="DateTime" name="LastModifiedDate" />
<many-to-one name="Reservation" column="ReservationId" class="Reservation" insert="true" update="false" />
<bag name="Approvals" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="ReservationEventId"/>
<one-to-many class="ReservationEventApproval"/>
</bag>
</class>
<class name="ReservationEventApproval" table="ReservationEventApproval">
<id name="Id" column="ReservationEventApprovalId" type="Int32" unsaved-value="0">
<generator class="native"/>
</id>
<many-to-one name="ReservationEvent" column="ReservationEventId" class="ReservationEvent" insert="true" update="false" />
<property column="ReservationEventApprovalStatusId" type="Int32" name="ReservationEventApprovalStatusId" />
<property column="LastModifiedDate" type="DateTime" name="LastModifiedDate" />
</class>