I have a class structure like so:
public class Person
{
public virtual int Id { get; protected internal set; }
public virtual string Name { get; set; }
}
public class Customer : Person
{
public virtual string AccountNumber { get; set; }
}
Customer
is mapped as a subclass of Person
(using FluentNHibernate's SubclassMap<T>
), and the table structure is table-per-subclass (separate Person
and Customer
tables sharing an Id
column).
In my test, I open a stateless session and try to insert a series of Person
entities:
using (var stateless = sessionFactory.OpenStatelessSession())
using (var transaction = stateless.BeginTransaction())
{
var persons = new[]
{
new Person { Name = "Alice" },
new Person { Name = "Bob" },
new Customer { Name = "Cathy", AccountNumber = "001" },
new Customer { Name = "Dave", AccountNumber = "002" }
};
foreach (var person in persons)
stateless.Insert(person);
transaction.Commit();
}
If I run this with the ShowSql
switch on, I can see that there are no INSERT
statements generated on the Person
table (meaning that they are batched), but there are individual INSERT
statements being generated for the Customer
table (from which I infer that these are not being batched).
Oddly enough, I've found that if the derived type (i.e. Customer
) has its own collection (let's call them Orders
) and I Insert
each item in that collection directly into the same stateless session (not into the actual collection), it batches those too and has no trouble resolving the relationships. This behaviour seems to be entirely limited to derived classes of polymorphic entities.
Is this expected behaviour? If so, is there any way that I can rewrite the insertion code above to ensure that all of the subclass tables are batched as well?
(Note: I am using the SequenceHiLoGenerator
for all IDs, and I've configured the AdoNetBatchSize
accordingly, so this is not a general issue with batching, as far as I can tell. I can see the HiLo
table being hit when a batch operation would be occurring.)