i faced this sort of problem with nhibernate:
when i try to delete a component (object that as no ID) that has null as value of a property, operation fails because condition is translated as field = NULL
instead of Field IS NULL
i have the RilevanzaFinding
object mapped as component of Finding
:
HasMany<RilevanzaFinding>(x => x.Rilevanze)
.Access.CamelCaseField(Prefix.Underscore)
.Table("RilevanzaFinding_T045")
.KeyColumn("int_T045_IdFinding")
.Cascade.AllDeleteOrphan()
.AsSet()
.Component(fee =>
{
fee.References<Rating>(x => x.Rating).Column("int_T045_IdRating").Fetch.Join();
fee.Map(x => x.DataFine)
.Column("dte_T045_DataFine")
.CustomSqlType("Date");
fee.Map(x => x.Note)
.Column("nvc_T045_Note")
.Length(100000);
});
public class Finding : BaseObject<Finding, int>
{
private ICollection<RilevanzaFinding> _rilevanze = new List<RilevanzaFinding>();
public virtual IEnumerable<RilevanzaFinding> Rilevanze
{
get
{
return _rilevanze.ToArray();
}
}
}
public class RilevanzaFinding : EquatableObject<RilevanzaFinding>
{
public virtual Rating Rating { get; set; }
public virtual DateTime? DataFine{ get; set; }
public virtual string Note { get; set; }
}
where EquatableObject implements this Equals:
public override bool Equals(object obj)
{
if (obj == null)
return false;
TObject other = obj as TObject;
return Equals(other);
}
public virtual bool Equals(TObject other)
{
if (other == null)
return false;
Type t = GetType();
TypeInfo typeInfo = t.GetTypeInfo();
IEnumerable<FieldInfo> fields = typeInfo.DeclaredFields.Where(x => x.FieldType.Name != typeof(ICollection<>).Name);
foreach (FieldInfo field in fields)
{
object value1 = field.GetValue(other);
object value2 = field.GetValue(this);
if (value1 == null)
{
if (value2 != null)
return false;
}
else if (!value1.Equals(value2))
return false;
}
return true;
}
now when i remove a Rilevanza from a collection of a finding Rilevanze tis is the sql generated by nhibernate:
NHibernate: DELETE FROM RilevanzaFinding_T045 WHERE int_T045_IdFinding =
@p0 AND dte_T045_DataFine = @p1 AND nvc_T045_Note = @p2 AND int_T045_IdRating
= @p3;@p0 = 201 [Type: Int32 (0:0:0)], @p1 = NULL [Type: DateTime (0:0:0)], @p2 = 'GD675PFN2GTR9EUJ3JHPG7XFX' [Type: String (1073741823:0:0)],
@p3 = 243 [Type: Int32 (0:0:0)]
wich fails due to "dte_T045_DataFine = NULL"
condition, since it would be "dte_T045_DataFine IS NULL"
how can i make it write the right condition?