When I’m clearing a childcollection like this
table.Indizes.Clear();
session.Flush();
then NH generates a delete SQL for each item which was in the collection:
DELETE FROM x_inddef WHERE ind_name = ‘IDX_ADRKONZ_CODE’ AND tbl_name = ‘ADRESSE’
DELETE FROM x_inddef WHERE ind_name = ‘IDX_ADRKUND_EXT’ AND tbl_name = ‘ADRESSE’
…
Why isn’t it generating a statement like this?
DELETE FROM x_inddef WHERE tbl_name = 'ADRESSE'
Is something wrong with my mappings, or is this just the normal behaviour?
Simplified Code with the fluent Mapping:
public class Table
{
public virtual string Name {get;set;
public virtual IList<Index> Indizes { get; set; }
}
public class TableOverride : IAutoMappingOverride<Table>
{
public void Override(AutoMapping<Table> mapping)
{
mapping.Table("x_tables");
mapping.Id(x => x.Name, "tbl_name");
mapping.HasMany(x => x.Indizes).KeyColumn("tbl_name").Inverse().Cascade.AllDeleteOrphan();
}
}
public class Index
{
public virtual string Name { get; set; }
public virtual Table Table { get; set; }
public override bool Equals(object obj)
{
//...
}
public override int GetHashCode()
{
//...
}
}
public class IndexOverride : IAutoMappingOverride<Index>
{
public void Override(AutoMapping<Index> mapping)
{
mapping.Table("x_inddef");
mapping.CompositeId().
KeyProperty(x => x.Name, "ind_name").
KeyReference(x => x.Table, "tbl_name");
}
}
You should enable batch updates option with nhibernate
First of all set
adonet.batch_sizeproperty in your NHibernate configuration to value, greater then zero.And then mark each hasMany collection with
.BatchSize(xxx)I guess this should help