2
votes

I'm getting an error when processing a collection of objects

Here is my code:

Toma.cs

public class Toma
{
    public virtual int TomaID { get; protected set; }
    public virtual DateTime FechaCarga { get; set; }
    public virtual String TipoDocumento { get; set; }
    public virtual String NroDocumento { get; set; }
    public virtual Boolean Procesada { get; set; }

    private IList<Dedo> dedos;
    public virtual IList<Dedo> Dedos 
    {
        get { return dedos; }
        protected set { dedos = value; }
    }
    private IList<Firma> firmas;
    public virtual IList<Firma> Firmas 
    {
        get { return firmas; }
        protected set { firmas = value; } 
    }
    private IList<Foto> fotos;
    public virtual IList<Foto> Fotos 
    {
        get { return fotos; }
        protected set { fotos = value; }
    }

    public Toma()
    {
        dedos = new List<Dedo>();
        firmas = new List<Firma>();
        fotos = new List<Foto>();
    }

    public virtual void MarcarComoProcesada()
    {
        this.Procesada = true;
        this.Dedos[0].Imagen = new Byte[] { 0x20 };
        this.Firmas[0].Imagen = new Byte[] { 0x20 };
        this.Fotos[0].Imagen = new Byte[] { 0x20 };
        //throw new NotImplementedException();
    }

    internal static IList<Toma> GetAll()
    {
        throw new NotImplementedException();
    }
}

Dedo.cs

public class Dedo
{
    public virtual int DedoID { get; protected set; }
    public virtual Byte[] Imagen { get; set; }
    public virtual Toma Toma { get; set; }
}

Firma.cs

public class Firma
{
    public virtual int FirmaID { get; protected set; }
    public virtual Byte[] Imagen { get; set; }
    public virtual Toma Toma { get; set; }
}

Foto.cs

public class Foto
{
    public virtual int FotoID { get; protected set; }
    public virtual Byte[] Imagen { get; set; }
    public virtual Toma Toma { get; set; }
}

My mappings:

Toma.cs

public class TomaMap : ClassMap<Toma>
{
    public TomaMap()
    {
        Table("Bio_Toma");
        Id(x=>x.TomaID).Column("TomaId");
        Map(x => x.FechaCarga);
        Map(x => x.TipoDocumento);
        Map(x => x.NroDocumento);
        Map(x => x.Procesada);
        HasMany(x => x.Dedos).Inverse().Cascade.All().KeyColumn("DedoID").Inverse();
        HasMany(x => x.Firmas).Inverse().Cascade.All().KeyColumn("FirmaID").Inverse();
        HasMany(x => x.Fotos).Inverse().Cascade.All().KeyColumn("FotoID").Inverse();
    }
}

Dedo.cs

public class DedoMap : ClassMap<Dedo>
{
    public DedoMap()
    {
        Table("Bio_Dedo");
        Id(x => x.DedoID);
        Map(x => x.Imagen);
        References(x => x.Toma).Column("TomaID");
    }
}

Firma.cs

public class FirmaMap : ClassMap<Firma>
{
    public FirmaMap()
    {
        Table("Bio_firma");
        Id(x => x.FirmaID);
        Map(x => x.Imagen);
        References(x => x.Toma).Column("TomaID");
    }
}

Foto.cs

public class FotoMap : ClassMap<Foto>
{
    public FotoMap()
    {
        Table("Bio_Foto");
        Id(x => x.FotoID);
        Map(x => x.Imagen);
        References(x => x.Toma).Column("TomaID");
    }
}

And where I'm getting the error:

                foreach (Toma toma in tomas)
                {
                      ... some other things...

                    if(toma.Fotos != null)
                    {
                        foreach (Foto foto in toma.Fotos) // HERE
                            fs.Write(foto.Imagen);
                    }
                     .... more code ....
                }

Reviewing questions and making the changes suggested there, could not correct the error. I'm thinking if the error is the number of objects that have to process. The number of "Tomas" object is 41k. There is some kind of limit on the session object for these cases?

Edit: Exception

'NHibernate.Exceptions.GenericADOException' en NHibernate.dll

 Additional information:could not initialize a collection
 [MJYS.DGSPR.Infraestructura.Entidades.Toma.Firmas#1039][SQL: SELECT firmas0_.FirmaID
 as FirmaID1_, firmas0_.FirmaID as FirmaID1_0_, firmas0_.Imagen as Imagen1_0_,
 firmas0_.TomaID as TomaID1_0_ FROM Bio_firma firmas0_ WHERE firmas0_.FirmaID=?]
1
Could you post the full exception(inner exception, stack trace, etc.) ? - Bun
Edited with the exception and the info I can get. - sebastianl

1 Answers

2
votes

The problem is hidden in missunderstanding of the KeyColumn. The <key column""> is used, in case of HasMany, as a name of column inside of the collection table, referring back to parent table.

In our scenario, it would most likely be column TomaID, so the mapping should be:

HasMany(x => x.Dedos)
  //.Inverse().Cascade.All().KeyColumn("DedoID").Inverse();
  .Inverse().Cascade.All().KeyColumn("TomaID").Inverse();
HasMany(x => x.Firmas)
  //.Inverse().Cascade.All().KeyColumn("FirmaID").Inverse();
  .Inverse().Cascade.All().KeyColumn("TomaID").Inverse();
HasMany(x => x.Fotos)
  //.Inverse().Cascade.All().KeyColumn("FotoID").Inverse();
  .Inverse().Cascade.All().KeyColumn("TomaID").Inverse();

This mapping is saying: NHibernate, when searching for children of soma Tomas object, take its ID, and search for it inside of the target table - column TamaID.

And that finally makes a circle of life complete, because that column must be the same as we've used in inverse mapping on each of the child collection Entities:

...
References(x => x.Toma).Column("TomaID");