1
votes

I'm quite new to nhibernate, I was doing all right until I face this problem, It looks like a NHibernate bug, but being a newbie with it, it can certainly be my fault

Having this base class to do all the Id and equality stuff

    public abstract class ObjetoConId
{
    public ObjetoConId()
    {
        Id=NewId();
    }

    public virtual Guid Id {get;private set;}

    public override bool Equals(object o)
    {
        if (Object.ReferenceEquals(this,o))
            return true;

        if (o==null) return false;

        ObjetoConId oId;

        oId= o as ObjetoConId;
        if (!Object.ReferenceEquals(oId,null))
            return (Id.Equals(oId.Id));

        return (base.Equals(o));
    }
    public override int GetHashCode()
    {
        byte[] bId;

        bId=Id.ToByteArray();

        return  ((Int32)(bId[8]^bId[12])<<24) +
                ((Int32)(bId[9]^bId[13])<<16) + 
                ((Int32)(bId[10]^bId[14])<<8) + 
                ((Int32)(bId[11]^bId[15]));
    }

    public virtual bool Equals(ObjetoConId o)
    {
        if (Object.ReferenceEquals(this,o))
            return true;

        if (Object.ReferenceEquals(o,null)) return false;
            return (Id.Equals(o.Id));
    }

    public virtual string toString() 
    {
        return this.GetType().FullName
        + "[id=" + Id + "]";
    }

    protected virtual Guid NewId()
    {
        return GuidComb.NewGuid();
    }

    public static bool operator == (ObjetoConId x,ObjetoConId y)
    {
        if(Object.ReferenceEquals(x,y))
            return true;
        if(Object.ReferenceEquals(x,null))
            return false;
        return x.Equals(y);
    }

    public static bool operator != (ObjetoConId x,ObjetoConId y)
    {
        return !(x==y);
    }

    /// <summary>
    /// Metodo interno para permitir el testing
    /// </summary>
    /// <param name="id"></param>
    internal void setId(Guid id)
    {
        Id=id;
    }
}

and this entity

public class Propiedad : ObjetoConId,IPropiedad
{
    [Obsolete("Persistance Constructor only")]
    public Propiedad ()
    {
    }


    public Propiedad (IList<IDescripcionCalificada> descripciones)
    {
        Descripciones=new Dictionary<string,IDescripcionCalificada>(descripciones.Count);
        foreach(IDescripcionCalificada d in descripciones)
            Descripciones.Add(d.Nombre,d);
    }


    #region IPropiedad implementation
    public virtual IDictionary<string, IDescripcionCalificada> Descripciones {get;private set;}

    #endregion  

}

and this mapping

    public class MapeoPropiedad : ClassMap<Propiedad>
{ 
    public MapeoPropiedad()
    {
        Id(x => x.Id).Column("pro_id").GeneratedBy.Assigned();
        HasMany<DescripcionCalificada>(x => x.Descripciones)
        .Cascade.SaveUpdate()
        .AsMap<string>(index => index.Nombre)
        ;
    }
}

The test for it is

[TestFixture]
public class TestPropiedadPersistencia
{

    [TestFixtureSetUp]
    public void TestFixtureSetUp()
    {

        string connectionString="Server=127.0.0.1;Database=Ana;User ID=dev-test;Password=dev-test;";

        fcfg=Fluently.Configure()
            .Database(PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString))
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MapeoPropiedad>());


        fcfg.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(false, true));
        sessions=fcfg.BuildSessionFactory();
    }


    ISessionFactory sessions;
    FluentConfiguration fcfg;



    [TestFixtureTearDown]
    public void TestFixtureTearDown()
    {
        fcfg.ExposeConfiguration(cfg => new SchemaExport(cfg).Drop(false, true));
        sessions.Close();
        sessions = null;
        fcfg = null;
    }


    [Test]
    public void CanCorrectlyMapPropiedad()
    {
        DescripcionCalificada descri1=new DescripcionCalificada("descri",new Descripcion("Esta es la descri"));
        DescripcionCalificada descri2=new DescripcionCalificada("descriLarga",new Descripcion("Esta es la descriLarga"));
        Dictionary<string,IDescripcionCalificada> descris=new Dictionary<string, IDescripcionCalificada>(2);

        descris.Add(descri1.Nombre,descri1);
        descris.Add(descri2.Nombre,descri2);

        new PersistenceSpecification<Propiedad>(sessions.OpenSession(),new CustomEqualityComparer() )
            .CheckProperty(c => c.Descripciones,descris)
            .VerifyTheMappings();
    }

}

The thing is that the test fails unless I put Not.LazyLoad() in the mapping It gives a mapping error

Ana.Nucleo.Lenguaje.Test.TestDescripcionCalificadaPersistencia (TestFixtureSetUp):
FluentNHibernate.Cfg.FluentConfigurationException : An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.


  ----> NHibernate.InvalidProxyTypeException : The following types may not be used as proxies:
Ana.Catalogacion.Implementacion.Propiedad: method setId should be 'public/protected virtual' or 'protected internal virtual'

without lazy loading it pass, and if I put the Id property in the Propiedad class and not inherit from ObjetoConID it also pass, with and without the Not.LazyLoad().

Anyone can confirm this is a NH bug, or any help will be appreciated

EDIT:

I've found the problem, my fault. I missed the setId internal function not being virtual protected and confused with the setter of the Id property, and thus missunderstood the execption

Fer

1

1 Answers

1
votes

I've found the problem, my fault. I missed the setId internal function not being virtual protected and confused with the setter of the Id property, and thus missunderstood the execption