0

I'm trying to delete an object and I receive a SqlTypeException : SqlDateTime Overflow.

So, today I activated the "Show SQL" property in the configuration, and to my surprise I discovered that when I try delete this entity NHibernate actually does an update witch results in the error because my DataTime property has an invalid value. The question is why is NHibernate trying to update the entity before the delete operation? Here is my code:

  using (ITransaction tnx = presentationSession.BeginTransaction()) { 
                try { 
                    presentationSession.Delete(View.SelectedData); 
         /* View.SelectedData has the reference I want to delete. Entity Id: 4601 */
                    tnx.Commit(); 
                } catch { 
                    tnx.Rollback(); 
                    throw; 
                } 
            } 

This is my mapping:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default- 
lazy="false"> 
    <class 
                name="PTFS.Personal.Model.Empleado, PTFS.Personal" 
                table="Supervisores" 
                > 
        <id 
            name="Id" 
            unsaved-value="0" 
            access="field.camelcase" 
            > 
            <generator class="native" /> 
        </id> 
        <property name="Nombre" /> 
        <property name="Supervisor" /> 
        <property name="Cedula" /> 
        <property name="Cargo" /> 
        <property name="Localidad" /> 
        <property name="Traslado" column="DES_TRASLADO" /> 
        <property name="Cambio" column="DES_CAMBIO" /> 
        <property name="FechaTraslado" column="F_TRASLADO" /> 
        <property name="FechaCambio" column="F_CAMBIO" /> 
        <property name="Ingreso" column="F_INGRESO" /> 
        <property name="Egreso" column="F_EGRESO" /> 
        <property name="Sueldo" column="SUELDO_BRUTO" /> 
    </class> 
</hibernate-mapping>

And this is the output I get when I commit the changes with the above code:

NHibernate: UPDATE Supervisores SET Nombre = @p0, Supervisor = @p1, 
Cedula = @p2, Cargo = @p3, Localidad = @p4, DES_TRASLADO = @p5, 
DES_CAMBIO = @p6, F_TRASLADO = @p7, F_CAMBIO = @p8, F_INGRESO = @p9, 
F_EGRESO = @p10, SUELDO_BRUTO = @p11 WHERE Id = @p12;@p0 = NULL, @p1 = 
NULL, @p2 = NULL, @p3 = NULL, @p4 = NULL, @p5 = NULL, @p6 = NULL, @p7 
= NULL, @p8 = NULL, @p9 = 05/09/2005 12:00:00 a.m., @p10 = NULL, @p11 
= 0, @p12 = 3547

NHibernate: UPDATE Supervisores SET Nombre = @p0, Supervisor = @p1, 
Cedula = @p2, Cargo = @p3, Localidad = @p4, DES_TRASLADO = @p5, 
DES_CAMBIO = @p6, F_TRASLADO = @p7, F_CAMBIO = @p8, F_INGRESO = @p9, 
F_EGRESO = @p10, SUELDO_BRUTO = @p11 WHERE Id = @p12;@p0 = NULL, @p1 = 
NULL, @p2 = NULL, @p3 = NULL, @p4 = NULL, @p5 = NULL, @p6 = NULL, @p7 
= NULL, @p8 = NULL, @p9 = 01/01/0001 12:00:00 a.m., @p10 = NULL, @p11 
= 0, @p12 = 4628

It is first updating two different entities... and none of then are the one I want to delete, the Id of the entity I provide for deleting is: 4601 Please, any help with this will be greatly appreciated.

jhenriquez
  • 181
  • 9

1 Answers1

1

NHibernate is updating those entities because the session you are using, had loaded them at some point. When it loaded them, they were stored in the 1st level cache. Then subsequently they were modified. When you then commit your transaction NHibernate flushes the session, thereby checking if any entities in its cache are "dirty" (ie need to be updated), and updates any of these entities.

So check if you're loading these entities at some point during the life-cycle of your Session.

Vadim
  • 17,897
  • 4
  • 38
  • 62
  • those entities ware loaded with that same ISession right before I tried deleting one of then. But I'm sure I did not modify any. Anyways, I'll try to review the code to make sure I'm not updating the entities. – jhenriquez May 16 '11 at 18:33
  • @jhenriquez, You might not be explicitly modifying them, but if some values are nullable in the database, but not nullable in your model (like Ingreso appears to be) NHibernate will think that the value is modified. – Vadim May 16 '11 at 18:43
  • Thanks very much! you pointed me in the right direction. Considering my current situation I decided to use a different session just for deleting the entity, so it doesn't need to flush any changes. – jhenriquez May 16 '11 at 19:59