我正在努力解决与父子映射相关的问题。 我认为我做的一切都正确,但每次添加一个子节点并保存父节点时,nhibernate都会执行插入操作,然后对子列表中的所有行执行更新。 这是我的课程和地图
public class Cliente : EntityBase
{
public virtual string Cognome { get; set; }
public virtual string Nome { get; set; }
public virtual int AttivitaId { get; set; }
public virtual DateTime? DittaAssunzioneData { get; set; }
public virtual decimal Stipendio { get; set; }
public virtual IList<ClienteChiusura> Chiusure { get; protected set; }
public virtual void AddTelefonata(Telefonata telefonata)
{
if (Telefonate == null)
Telefonate = new List<Telefonata>();
//NHibernate impedence
telefonata.Cliente = this;
Telefonate.Add(telefonata);
}
public virtual void RemoveTelefonata(int telefonataId)
{
if (Telefonate == null)
return;
var telefonata = Telefonate.SingleOrDefault(x => x.Id == telefonataId);
if (telefonata != null)
{
Telefonate.Remove(telefonata);
telefonata.Cliente = null;
}
}
}
public class Telefonata : EntityBase
{
public virtual Cliente Cliente { get; set; }
public virtual string Descrizione { get; set; }
public virtual DateTime? Data { get; set; }
public virtual bool Ingresso { get; set; }
public virtual string Utente { get; set; }
public virtual DateTime? UtenteData { get; set; }
public virtual TelefonataTipo TelefonataTipo { get; set; }
}
public class TelefonataTipo : EntityBase
{
public virtual string Nome { get; set; }
}
public abstract class EntityBase
{
public virtual int Id { get; private set; }
//ctor
protected EntityBase()
{ }
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
return (obj != null && obj.GetType() == GetType() && ((EntityBase)obj).Id == Id);
}
public static bool operator ==(EntityBase entity1, EntityBase entity2)
{
//cast come object altrimenti ho un loop ricorsivo
if ((object)entity1 == null && (object)entity2 == null)
return true;
if ((object)entity1 == null || (object)entity2 == null)
return false;
if (entity1.GetType() != entity2.GetType())
return false;
if (entity1.Id != entity2.Id)
return false;
return true;
}
public static bool operator !=(EntityBase entity1, EntityBase entity2)
{
return (!(entity1 == entity2));
}
}
映射如下
public class ClienteMapping : MappingBase<Evoltel.PuntoQuinto.Domain.Entities.Cliente.Cliente>
{
public ClienteMapping()
: base("CLIENTI")
{
Id(x => x.Id, c => { c.Column("CLIENTE_ID"); c.Generator(Generators.Native, g => g.Params(new { sequence = "GEN_CLIENTI_ID" })); });
Property(x => x.Cognome, c => c.Column("COGNOME"));
Property(x => x.Nome, c => c.Column("NOME"));
Property(x => x.AttivitaId, c => c.Column("ATTIVITA_ID"));
Property(x => x.DittaAssunzioneData, c => c.Column("D_ASSUNZIONE_DATA"));
Property(x => x.Stipendio, c => c.Column("D_STIPENDIO"));
Bag(x => x.Telefonate, map =>
{
map.Inverse(true);
map.Table("TELEFONATE");
map.Cascade(Cascade.All | Cascade.DeleteOrphans);
map.Key(k => k.Column("CLIENTE_ID"));
map.Lazy(CollectionLazy.NoLazy);
}, r => r.OneToMany());
}
public class TelefonataMapping : MappingBase<Evoltel.PuntoQuinto.Domain.Entities.Cliente.Telefonata>
{
public TelefonataMapping()
: base("TELEFONATE")
{
Id(x => x.Id, c => { c.Column("TELEFONATA_ID"); c.Generator(Generators.Native, g => g.Params(new { sequence = "GEN_TELEFONATE_ID" })); });
ManyToOne(x => x.Cliente, c => c.Column("CLIENTE_ID"));
Property(x => x.Descrizione, c => c.Column("DESC_LUNGA"));
Property(x => x.Data, c => c.Column("TELEFONATA_DATA"));
Property(x => x.Ingresso, c => { c.Column("INGRESSO"); c.Type<TrueFalseType>(); });
Property(x => x.Utente, c => c.Column("UTENTE_NOME"));
Property(x => x.UtenteData, c => c.Column("UTENTE_DATA"));
ManyToOne(x => x.TelefonataTipo, c => { c.Column("TELEFONATA_TIPO_ID"); c.NotFound(NotFoundMode.Ignore); });
}
}
public abstract class MappingBase<T> : ClassMapping<T> where T : EntityBase
{
protected MappingBase(string tableName)
{
Table(tableName);
Lazy(false);
DynamicUpdate(true);
}
}
如果我不使用Cascade.All新的Child没有保存(而且我对NHibernate的理解是正确的)
我没看到什么?